Question
IN PYTHON The number guessing game allows the user to enter a guess for a number that the computer generates between 1 and 100. The
IN PYTHON
The number guessing game allows the user to enter a guess for a number that the computer generates between 1 and 100.
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 100 Coins.
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, 100))
Write another function that takes a user's guess and the current 'secret' guess and if the user's guess is correct update the TotalCoins (this must be a global variable) and then return True, otherwise return False if the user's guess does not match the secret guess.
Call this function CheckingTheGuess
Now you write code for main:
def main(): #note this function will always have no parameters by convention
print(" Its time to play The Guessing Game")
UserGuess = int(input(" Try to guess: ")) #note you must convert the input to int since the input returns.a string
ComputerGuess = #Call the GenerateGuess to return a value in this ComputerGuess variable
if CheckingTheGuess(........,.......) #Pass CheckingTheGuess the computerGuess and the UserGuess. It will return True or False
#If CheckingTheGuess returns True, display congrats and show total coins.
#otherwise display a you lost msg.
#Ask if the user wants to play again. If user types '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 TotalCoins won.
Note when you call main from main, it gives the user the appearance that the game is repeating.
You need to write the CheckingTheGuess function to return True if the user's guess parameter is the same as the computer's guess parameter and increment the total points. Otherwise this function returns False.
You also need the TotalCoins 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.
Once you finish writing the code make sure to test it properly (and many times providing wrong and correct guess and add up your coins) before uploading.
Note to test your code, you must know what the computer's guess is. For testing purposes, use a print statement inside the GenerateGuess function but remove it after you are done testing.
Add comments as you see fit but make sure to add top level comments.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started