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
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