问题描述
我在使用while循环的简单代码中遇到问题。 我的问题在代码注释中说明。
码
exit = False
while not exit:
choice = input("Test ")
if choice== 1:
print "hello"
exit = False
else:
print "good morning"
#I want to return to the first while with the input Test but I pass to the second while
exit = False
exit1 = False
while not exit1:
choice = input("Test 1")
if choice== 1:
print "bye"
exit1 = False
else:
print "good evening"
#I want to return to the first while with the input Test but I return to the second while
exit = False
非常感谢。
1楼
我认为您正在寻找的是continue
和break
语句。
continue
将中断当前的迭代(当然,新的迭代将开始)。
break
将中断最小的封闭循环。
这两种报表工作for
为好。
看看以供参考。
2楼
outer = True
while outer:
do_something
if a:
continue # will get you back to do_something
if b:
break # will end the outer loop
如果设置outer
,以False
的地方,它将结束在下次迭代while循环。