Code a guessing game in Python Parameters: The number guessing game allows the user to enter a guess for a number that the computer generates between 1 and 10. The user can make only one guess per game. If the guess is incorrect, the game ends. If a guess is correct the user wins. Either way the user may play again. Each time the user wins. they collect 5 points. To implement this game, write code for the following: GenerateGuess() which is a function that generates a random guess. I will give you the code for this function. But at the very top of the code page you must type: from random import randint #this is python code so follow the case properly and this is the top line with no indentation
Code a guessing game in Python
Parameters:
- The number guessing game allows the user to enter a guess for a number that the computer generates between 1 and 10.
- The user can make only one guess per game. If the guess is incorrect, the game ends. If a guess is correct the user wins. Either way the user may play again.
- Each time the user wins. they collect 5 points.
To implement this game, write code for the following:
- GenerateGuess() which is a function that generates a random guess. I will give you the code for this function. But at the very top of the code page you must type:
- from random import randint #this is python code so follow the case properly and this is the top line with no indentation
#then here is that GenerateGuess
def GenerateGuess():
return (randint(1, 10))
Write another function that takes a user's guess and the current 'secret' guess and if the user's guess is correct , add the points to their score and return True, otherwise return False.
Call this CheckGuess
Now you write code for main:
def main():
print("Let's play the number Guessing Game")
UserGuess = int(input("Make a guess: "))
ComputerGuess = #Call the GenerateGuess to return a value in this ComputerGuess variable
if CheckGuess(........,.......) #Pass CheckGuess the computerGuess and the UserGuess. It will return True or False
#If CheckGuess returns True, add 5 points to the user TotalPoints. This variable must be global.
#otherwise display you lost msg.
#Ask if the user wants to play again. If yes, call main.
# If user does not want to play again, don't call main. the code will end. Just display a good bye msg with the TotalPoints won.
Note when you call main from main, it gives the user the appearance that the game is repeating.
You need to write the CheckGuess function to return True if the user's guess parameter is the same as the computer's guess parameter. Otherwise this function returns False.
You also need the TotalPoints variable to be initialized at 0 at the top. Then use the global keyword each time you want to use that variable in any other function. This way this variable will retain its content.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps