假设 SECRET_NUMBER = 77。我希望该函数不断提示用户,直到猜到密码为止。但是,“您的猜测太低”或“您的猜测太高”无法正常工作。如果我输入guess_number(4),它说猜测太低,但如果我接下来输入100,它仍然说我的猜测太低。我的功能可能有什么问题?
def guess_number(num):
'''(int) -> NoneType
Print out whether the secret number was guessed or a hint, if the
number was not guessed. Make this prompt the user for a number until the
secret number is guessed.
>>> guess_number(50)
Your guess was too low!
'''
while num != SECRET_NUMBER:
if num < SECRET_NUMBER:
print('Your guess was too low!')
input('Guess a number: ')
elif num > SECRET_NUMBER:
print('Your guess was too high!')
input('Guess a number: ')
else:
print('You guessed it!')