Python 如何返回到以前嵌套的'while True'循环?
假设我有这种格式的东西:Python 如何返回到以前嵌套的'while True'循环?,python,python-3.x,loops,while-loop,nested-loops,Python,Python 3.x,Loops,While Loop,Nested Loops,假设我有这种格式的东西: while True1: if something: do some thing elif something else: do something else while True2: if something1: do some thing1 if something2: do some thi
while True1:
if something:
do some thing
elif something else:
do something else
while True2:
if something1:
do some thing1
if something2:
do some thing2
if want to end this while True2 loop
go back to first while True1 loop
elif something else else:
do some thing else else
等等
如何使while True
循环中的某个内容返回到嵌套在其中的前一个while True
循环?使用该语句
x = 0
while True:
x += 1
if x == 5:
print(x)
break
将输出
5
并退出,break
语句就是这样做的
while True: #1
if something:
do something
elif something else:
do something else
while True: #2
if something1:
do something
if something2:
do something else
if want to end this while True #2 loop:
break # will break out of the innermost loop only
elif something else else:
do some thing else else
阅读更多信息:
中断
将中断当前循环