Question: The python code below is for a reverse guessing game in which the computer tries to guess the number a person has in their mind.

The python code below is for a reverse guessing game in which the computer tries to guess the number a person has in their mind. How can the number of guesses the computer has be limited to 3? Anyway to use the for statement or range function to limit the number of guesses to 3. Currently, it keeps guessing till it gets the correct answer.

print('Think of a number between 1 and 10.') input('Once ready hit any key ...')

low = 1 high = 10 guessed_correct = False while low <= high: mid = low + (high - low) // 2 response = input(' Is your number: {} (yes or no)? '.format(mid)).lower() if response == 'yes': print(" Game Over. I win.") guessed_correct = True break elif response == 'no': response = input(f'Is {mid} high or low than your number? ') if response == 'high': high = mid - 1 elif response == 'low': low = mid + 1 else: print('Error: Invalid response. Please enter yes or high or low only.')

if not guessed_correct: print('You must be cheating ...')

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!