Question: A Guessing Game (Python) In this game, the computer selects a single number between 1 and 100 called the target number. Different players are then

A Guessing Game (Python)

In this game, the computer selects a single number between 1 and 100 called the target number. Different players are then asked to guess the number. If the guess is higher or lower than the target number, the computer announces which it is, and its then the next players turn. If a player guesses the exact number, the computer announces the victory. The game starts by prompting for the total number of players. Each player is prompted in turn for their guess. The following demonstrates correct operation of the code for a target number of 73:

Enter number of players: 3

Im thinking of a number between 1 and 100. Guess what it is.

Player 1? 50

Too low.

Player 2? 75

Too high.

Player 3? 62

Too low.

Player 1? 68

Too low.

Player 2? 71

Too low.

Player 3? 73 ***PLAYER 3 WINS!*** Note that your program must cycle through the players. Assume the variable num players is the number of players; then you can update the player number at the completion of each turn using the following statement: player = (player + 1) % num_players

Write a program called guess num.py. Youll need to import the randrange() function from the random module as the rst line of your code. To do so, use the following: from random import randrange.Your program must do the following: Obtain a value for the target number from the randrange() function. This number should be between 1 and 100. Prompt for the total number of players (num players). Print the message, Im thinking of a number between 1 and 100. Guess what it is. Initialize the player number player to 0. Enter a for-loop that iterates up to 100 times. The rst statement in the body of the for-loop should prompt the appropriate player for their guess. It might be less confusing to create the prompt outside the input() function. For example, prompt = Player + str(player + 1) + ? Once a guess has been obtained, its compared with the target number. If its too high, the message Too high. is printed. If its too low, the message Too low. is printed. If the guess is correct, the program announces the winning player and a break statement is used to exit the loop which ends execution of the program. If a guess isnt correct, the player number is set to the next player (as specied above).

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 Databases Questions!