为什么while循环无法中断?
Why is the while loop unable to break?
answer = 5
guess = int(input('Please make a wild number guess: '))
count = 1
while guess != answer:
count += 1
int(input('wrong. Please make another guess: '))
print(f"this is your {count} attempt")
if guess == answer:
break
print('Correct!!!')
输入 5 后,我没有得到预期的答案。输入正确答案后,我仍然卡在 while 循环中。
错了。请再猜一猜:5
这是你的第 6 次尝试
因为您没有在 while 循环中更新“guess”的值,所以将其更改为
guess = int(input('wrong. Please make another guess: '))
如@Soulfly 所述,您需要更新 guess
。
此外,我建议更改:
if guess == answer:
break
print('Correct!!!')
到
if guess == answer:
print('Correct!!!')
break
因此在输出打印语句之前循环不会中断。
answer = 5
guess = int(input('Please make a wild number guess: '))
count = 1
while guess != answer:
count += 1
int(input('wrong. Please make another guess: '))
print(f"this is your {count} attempt")
if guess == answer:
break
print('Correct!!!')
输入 5 后,我没有得到预期的答案。输入正确答案后,我仍然卡在 while 循环中。
错了。请再猜一猜:5 这是你的第 6 次尝试
因为您没有在 while 循环中更新“guess”的值,所以将其更改为
guess = int(input('wrong. Please make another guess: '))
如@Soulfly 所述,您需要更新 guess
。
此外,我建议更改:
if guess == answer:
break
print('Correct!!!')
到
if guess == answer:
print('Correct!!!')
break
因此在输出打印语句之前循环不会中断。