gpt4 book ai didi

python - 当询问答案时循环超过最大尝试次数 (3)

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:14 25 4
gpt4 key购买 nike

在我的程序中,它应该向用户提出问题并给他们 3 次机会来猜出正确答案。但是我的“while”循环似乎给了用户第四次机会来回答问题并绕过“max_attempts”变量。

print('Quiz program!\n')
answer = input('What is the capital of Wisconsin? ')
attempt = 1
max_attempts = 4

while answer != 'Madison':
attempt += 1
print('You got it wrong, please try again.\n')
answer = input('What is the capital of Wisconsin? ')
if attempt == max_attempts:
print('You used the maximum number of attempts, sorry. The correct answer is "Madison"')
break
else:
print(f"Correct! Thanks for playing. It took you {attempt} attempt(s).")

最佳答案

以上所有答案都是正确的,只是添加了一个稍微不同的变体。

print('Quiz program!\n')
attempt = 1
max_attempts = 4

while attempt < max_attempts:
attempt += 1

answer = input('What is the capital of Wisconsin? ')
if answer == 'Madison':
print("Correct!")
break
else:
print('You got it wrong, please try again.\n')

print("Thanks for playing. It took you %s attempt(s)." %(attempt-1))

关于python - 当询问答案时循环超过最大尝试次数 (3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com