Question: Write a program that allows the user to play a number guessing game. Your program should randomly select a number from 1 to 1 0

Write a program that allows the user to play a number guessing game. Your program should randomly select a number from 1 to 1024(using random.randint()), and the user has 10 attempts to correctly guess the number. For each guess, the program should tell the user if their guess was too low or too high and then prompt the user for a new guess.
If the user guesses the number, then the program should congratulate them and report how many guesses it took. If the user does not guess the number within 10 guesses, the program should tell the user what the number was.
Use this starter code: starter code. Download this file, and modify it to complete the assignment. To download, ctrl-click (macOS) or right click (Windows) and select Save File As...(details may vary slightly depending on your browser).
Input validation
The users guesses should be
a number from 1 to 1024(inclusive), and
a number they havent guessed already (use a list to keep track guesses).
Invalid guesses should not count as a guess, should not be appended to the list of guesses, and the user should be prompted to guess again.
Constants
Notice that there are two constants provided:
MAX_GUESSES =10
UPPER_BOUND =2** MAX_GUESSES
You should use these in your code wherever appropriate to avoid magic numbers. Do not change these constants!
Functions
Your program should have two (impure) functions, guess and play. Each function has its own responsibilities. Often, when were designing computer programs we have to decide what functional decomposition makes sense. The specification for this program outlines the responsibilities of these two functions! Dont mix them up! Make sure you understand what each of these functions is supposed to do before you start writing code. There are extensive comments in the starter code provided.
guess(target, guesses)
This function should prompt the user for a guess, and validate the users guess. If the guess is valid (its in the desired range and hasnt already been guessed), the new guess should be appended to the list guesses. If the guess is invalid, the function should display a complaint starting with the word INVALID! with exclamation point, in ALL CAPS.
This function should return True if the guess is correct (it matches target), and False otherwise.
play(target)
This function should run the game. Within this function you should keep track of how many guesses the user has remaining, report the number of guesses the user has remaining (as shown above), and should call guess(target, guesses) in a loop. Once the call to this function has returned, youll know if the user has guessed the target, and youll have the most recent guess as the last element in guesses. If the user has guessed correctly, you should report that the user has won, giving the secret number (target) and number of guesses consumed. Example:
You WIN! The secret number was 542 and it took you 8 guesses.
Make sure you use the string WIN! in this output.
If the users guess is greater than the target, your function should report
Guess is too HIGH!
again, using HIGH! as shown.
If the users guess is less than the target, your function should report
Guess is too LOW!
again, using LOW! as shown.
If the user consumes all ten guesses without guessing correctly, your program should reveal the secret number. Example:
You LOSE! The secret number was 127.
The body of your program
The body of your program, nested under if __name__=='__main__': should begin with the starter code as provided. Do not modify the starter code provided. Beneath the starter code youll need only three more lines:
One line of code, should provide instructions to the user:
Try to guess the secret number from 1 to 1024.
One line of code should generate a pseudo-random number in the range 1 to 1024, using random.randint().
The one remaining line of code should call play() providing the random number as an argument.
Playing the game
Once you complete the progr

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!