- 24th Jul 2024
- 18:08 pm
In this assignment the hardest part of this game is working out the logic for checking how many the user has corrected and how many are in the wrong place. Using the example above, if the user enters 'apple', 'apple', 'apple', 'apple' they should see the messages, 'Correct fruits in the correct place: 2' and 'Correct fruits but in the wrong place: 0'. Decide if you want to allow upper and lower case or if it is easier to convert everything to the same case. Make sure you build in validation checks to make sure the user is only entering valid data and display a suitable message if they make an incorrect selection. If they do make a wrong selection, you may want to allow them to enter the data again, rather than class it as an incorrect guess.
3.0 Programming Requirements
In this programming project, you must apply and demonstrate the following programming techniques using Python programming language.
- Input and display data.
- Lists.
- Random choice from a list.
- If statement, and relational and logical operators.
- Loops.
- User-defined function.
You must explain the computational implementation and problem-solving techniques of these programming methods in the video presentation and document your explanation in the presentation slides. No non-Python native external packages are allowed to be used for this programming project. Only the random library (import random) is allowed for generating random values as required by the programming project.
Free Assignment Solution - Master Mind Computer Game - CSC1024
import random
# define a list name fruit_list which contains the name of the fruits
fruit_list = ['apple', 'orange', 'banana', 'grape', 'mango', 'strawberry', 'pear', 'pomegranate']
# this function print the list of fruits
def printFruitList():
print("Fruits Options: ")
for fruit in fruit_list:
print(fruit)
# this function generate a random number in a range between 0 and length of fruit list
def getRandomNumber():
number = random.randint(0, len(fruit_list)-1)
return number
def getRandomFruit():
return fruit_list[getRandomNumber()]
# this function return a list of computer of 4 fruits randomly generated
def getComputerList():
computer_list = []
for i in range(0, 4):
computer_list.append(getRandomFruit())
return computer_list
# this function get a fruit from user valid if it is present in fruit list and then return it
def getUserChoiceFruit():
while True:
user_choice_fruit = input("Enter the name of the fruit: ")
for fruit in fruit_list:
if user_choice_fruit.lower() == fruit:
return fruit
print("Invalid Fruit Name Entered You Need To Enter Again")
# this function plays the game until user guesses right
def playGame():
guess_count = 0 # variable to store the guess count of user
computer_list = [] # list to store computer choices
computer_list = getComputerList()
print("Games Starts")
print("Computer have already guessed four fruits randomly")
while True:
correct_place_count = 0
correct_count = 0
guess_count = guess_count + 1
user_list = []
correct_index = [] # list to store the fruits which are guessed right
print("User Turns\n")
printFruitList()
for i in range(0, 4):
user_list.append(getUserChoiceFruit())
for i in range(0, 4):
if user_list[i] == computer_list[i]:
correct_place_count = correct_place_count + 1
correct_index.append(i)
fruit_choice = []
for i in range(0, 4):
if i not in correct_index: # checking if the index already contains correct fruit
for j in range(0,4):
if j not in correct_index and j not in fruit_choice: # checking if the index already contains correct fruit
if user_list[i] == computer_list[j]:
correct_count = correct_count + 1
fruit_choice.append(j)
if correct_place_count == 4:
print("You Won Turns Taken:",guess_count)
break
else:
print("Correct fruits in the correct place:",correct_place_count)
print("Correct fruits but in the wrong place:",correct_count)
playGame()
Get the best Analyzing Spotify's Top 10 Songs For Event Management assignment help and tutoring services from our experts now!
This sample Python assignment solution has been successfully completed by our team of Python programmers. The solutions provided are designed exclusively for research and reference purposes. If you find value in reviewing the reports and code, our Python tutors would be delighted.
-
For a comprehensive solution package including code, reports, and screenshots, please visit our Python Assignment Sample Solution page.
-
Contact our Python experts for personalized online tutoring sessions focused on clarifying any doubts related to this assignment.
-
Explore the partial solution for this assignment available in the blog above for further insights.
About The Author - Alex Gen
Alex Gen is a seasoned Python developer skilled in implementing complex game logic and validation techniques. For this assignment, Alex tackled the Master Mind game, focusing on validating user inputs, calculating correct and misplaced guesses, and applying Python programming techniques. Alex utilized lists, random choices, conditionals, loops, and user-defined functions to create a robust game experience, ensuring clear error handling and user-friendly interactions.