I'm creating a program that asks you to think of a number from 0 to 100
. Then it will guess if it IS 50, lower than 50, or higher than 50
. The program will keep guessing with different numbers until it guesses the correct number.
If the user doesn't enter yes
, lower
or higher
, then the output is supposed to be "I did not understand". The problem with my code is that it gets an infinite loop. I assume there is a loop and I need to include something to end it once the user says "yes".
Here is what I have so far (I'm a novice programmer so I'm sorry if this doesn't make sense or if my code is really bad!):
print('Hello.')
print('Pick a secret number between 0 and 100.')
print('Is your secret number 50')
low = 0
high = 101
guess = 50
a = input('Enter yes/higher/lower:\n')
while True:
if a == 'yes':
print('Great!')
break
elif a == 'lower':
high = guess
guess1 = (guess-low)//2+low
print('Next is', guess1)
print('Is your secret number', guess1)
elif a == 'higher':
low = guess
guess1 = (high-guess)//2+guess
print('Next is', guess1)
print('Is your secret number', guess1)
elif a != 'yes' or 'higher' or 'lower':
print('I did not understand')