这只是我的代码的一部分,我不确定为什么我的 while 循环不允许用户重试。请帮助我!
answer3 = True
while answer3:
if answer2.lower() == "no" or answer2.lower() == "nah":
print ("Okay then ... Bye.")
sys.exit()
elif answer2 == "Yes".lower() or answer2.lower() == "yeah" or answer2.lower() == "yes":
print ("Okay then ... \n")
else:
print("Please enter a valid answer! Try again!\n")
break
请您参考如下方法:
break 函数退出 while 循环,因此即使 answer3 的值仍然为 True,它也会在第一个循环结束后停止循环。删除 break,它将起作用。
It terminates the current loop and resumes execution at the next statement, just like the traditional break statement in C.
The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.
If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.
