如果我有一段代码,看起来像这样:打破了多个嵌套而循环
for ship in ships.shipLengths.key():
while(True):
# Code
while(True):
# Code
while(True):
# Code
如果我在第三我目前while循环,有没有办法,我要回第一while循环?
如果我有一段代码,看起来像这样:打破了多个嵌套而循环
for ship in ships.shipLengths.key():
while(True):
# Code
while(True):
# Code
while(True):
# Code
如果我在第三我目前while循环,有没有办法,我要回第一while循环?
我同意@Aluan和@Daniel这样写代码不是一个好习惯。
无论如何,如果你仍然想这样做,这里是一个办法:
x = True
for ship in ships.shipLengths.key():
while condition1:
# Code
while condition2 and x:
# Code
while condition3 and x:
# Code
if goto_1st_while:
x = False
continue
# The code here will not run when goto_1st_while is True
if not x:
continue
# The code here will not run when goto_1st_while is True
继续不好,继续不能退出循环但只能继续其运行 – kouty
是的,但是当x为False时,将退出。 –
请不要写代码,这样,它有可怕的可读性。使用函数来代替每个循环的主体。 –
在函数中编写你的代码,所以你可以使用返回来退出多个循环 – Daniel
[如何摆脱Python中的多个循环?](https://stackoverflow.com/questions/189645/how-to-break -out-of-multiple-loops-in-python) –