在下面的代码中,当我输入正确答案时,“错误...再次猜测”不断打印。这是在第一个 if 语句中。我不知道如何设置一个条件,使其仅在答案错误时才打印。感谢您的帮助。
out_of_guesses = False
answer = 'dog'
max_guess = 3
guess = ''
guess_counter = 0
while guess != answer and not(out_of_guesses):
if guess_counter < max_guess:
guess = input('pick an animal')
guess_counter += 1
print ('wrong...guess again')
else:
out_of_guesses = True
if out_of_guesses:
print ('you lose')
else:
print('you win')
我输入了第一段的所有内容。
您将要打印的语句与输入放在同一个 if 块中,这意味着它会针对每个输入运行。您想将“错误”逻辑放置在输入逻辑之外。