重置一段时间 True 循环



你如何重置一个 while True 循环。例如,这是我的代码:

x=True
while x==True:
print ("Random Text")
randomtext= input()
if randomtext == "yes":
print ("hi")
x=False
elif randomtext == "no":
print("This is supposed to reset the loop")
#resets loop and prints Random Text again
print ("hi")
if x==True:
print ("placeholder text")
input()

如果"随机文本"等于是,我希望它重置循环。我想在循环中间重置它。这是一个非常简单的问题,但这正在进入我的编程。谢谢。

我假设通过重置循环,您意味着跳转代码直到再次到达循环的开头。这是通过"继续"关键字完成的。

if randomtext == "yes":
continue

如果你真的想打破循环,你应该使用"break"关键字。

最新更新