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