Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Game applications usually involve generating content that even the programmer can not know of . In this game, we will generate a random number. Even

Game applications usually involve generating content that even the programmer can not know of.
In this game, we will generate a random number. Even you, as you write the code won't know what the number is.
This is the guessing game with a twist.
Our number guessing game allows the user to decide the range of numbers to guess as long as that range is at least 10, meaning they need to guess a number out of ten numbers.
The user can make one guess per game. They earn 5 points for each correct guess.
Either way the user may play again.
To implement this game:
write code for a GenerateGuess(s,e) 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
The above line refers to file (named random) that is already built into python. It contains a built in function named randint(start, end) which returns a random number between start and end.
#then here is that GenerateGuess
def GenerateGuess(s,e):
if ------- : #fill in the blank so we test if s is less than e, and s is a positive number
if ---------: #fill in the blank to check if the distance from s to e is at least 10
return (randint(1, e)) #this generate a number between 1 and e.
return (----) #if the above does not return, it means one of the if's failed. In that case create a random number between 1 and 100
Write another function that takes two parameters: a user's guess and the current 'secret' guess.
if the user's guess is exactly the same as the computer guess return 10(the user gets 10 points for an exact guess).
Otherwise if the guess is within 5 points of the secret guess, then return 5(the user gets 5 points for an approximate guess).
Otherwise return 0.
Call this function CheckGuess
Write another function, UpdatePoints, that takes one parameter, and adds that value to the TotalPoints global variable.
You need the TotalPoints variable to be initialized at 0 at the top of your code file but not inside this function.
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.
There is a video on this in our lectures - to learn how to use the global keyword and assign a global variable.
Now you write code for main:
def main(): #note this function will always have no parameters by convention
print("Let's play the number Guessing Game")
UserGuessRange = int(input("What range do you want to play for? ")) #note you must convert the input to int since the input returns a string
ComputerGuess =........ #Call the GenerateGuess on 1 and UserGuessRange to return a value in this ComputerGuess variable
#here get a guess from the user to use next. Call this variable UserGuess
Status = CheckGuess(-------,--------) #Pass CheckGuess the computerGuess and the UserGuess. It will return 10,5 or 0
if Status ==0: #if 0 it means user did not guess right
# display a you lost msg.
else: #user guess was right. The value of Status tells us the points they earned
#activate UpdatePoints on the Status then print the total points
# 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 TotalPoints won.
Note when you call main from main, it gives the user the appearance that the game is repeating.
Once you finish writing the code make sure to test it properly (and many times providing wrong, exact, and approximate guesses and add up your points) 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. No top level comments means there will be points lost.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions