【发布时间】:2016-01-02 20:34:50
【问题描述】:
我正在尝试编写一个包含while 循环的程序,在这个循环中,如果出现问题,我会收到一条错误消息。有点像这样;
while True:
questionx = input("....")
if x =="SomethingWrongabout questionX":
print ("Something went wrong.")
continue
other codes...
questiony = input("....")
if y == "SomethingWrongabout questionY":
print ("Something went wrong.")
continue
other codes...
questionz = input("....")
if z == "SomethingWrongabout questionZ":
print ("Something went wrong.")
continue
other codes..
问题如下:当questionX之后出现错误时,程序进入开始。它从头开始,而不是从y 或z。但是x没有问题,所以程序应该从y或z开始提问,因为问题发生在y或z。
如何使程序从特定点开始,例如如果仅在yquestion 出现错误,则程序必须从y 开始提问,或者如果仅在z,程序必须从@ 开始987654334@,不是开始-不是x。
我应该为此使用多个while 循环,还是有什么东西可以使这个只在一个循环中工作?
【问题讨论】:
-
你为什么要在一个while循环中实现这个,或者这个特定的代码结构?如果您想重新启动完整的程序以记住最后一个正确回答的问题,为什么不使用循环外可用的变量,无论是在内存中还是在持久存储中?
标签: python loops python-3.x while-loop continue