如何 return 到以前嵌套的 `while True` 循环?
How to return to previously nested `while True` 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 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
循环?
使用break
语句。
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
阅读更多:Control Flow Tools
假设我有这种格式的东西:
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
循环?
使用break
语句。
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
阅读更多:Control Flow Tools