脱离多个嵌套while循环

2024-10-03 00:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一个如下所示的代码:

for ship in ships.shipLengths.key():
    while(True):
        # Code
        while(True):
            # Code
            while(True):
                # Code

如果我现在在第三个while循环中,有没有办法让我回到第一个while循环?在


Tags: key代码intrueforcodewhile办法
1条回答
网友
1楼 · 发布于 2024-10-03 00:18:42

我同意@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

相关问题 更多 >