Question: In Python, note the following code: # This is a guess the number game. import random #print('Hello! What is your name?') myName = input('Hello! What

In Python, note the following code:

# This is a guess the number game. import random

#print('Hello! What is your name?') myName = input('Hello! What is your name?')

number = random.randint(1, 20) print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

guessesTaken = 0

while 1: #print('Take a guess.') # There are four spaces in front of print. guess = int(input()) guessesTaken = guessesTaken + 1 if guess == number: break elif guess < number: print('Your guess is too low.')

elif guess > number: print('Your guess is too high.')

guessesTaken = str(guessesTaken) print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

####################################################################

Modify the Guess a Number program above.

a) Make the number of guesses a variable. So, the program asks the users how many guesses they will need to find the number. Based on the user's input, the user will have that many guesses to find the number.

Ask the player how may times he thinks he needs and use his answer

b) End the WHILE loop without using the break statement.

How does the where loop end??!!!

1. Use a break

2. Use a condition the becomes "False" at the desired situation

c) Use if else at least once.

d) Replace the last two if statements in the original code with a function. Define your function in the beginning of the code.

Note: I pasted the two if statements below.

#################

if guess == number: guessesTaken = str(guessesTaken) print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number: number = str(number) print('Nope. The number I was thinking of was ' + number)

#########################################################

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Lets modify the Guess the Number game to incorporate the suggested features Step 1 Initialize the Game Well start by importing the random module and i... View full answer

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!