跳出两个循环

Break outside of two loops

我正在编写一个在 while 循环中包含 while 循环的代码,我想知道如果在内循环中满足必要条件,如何跳出外循环。

while N <= 8:
    while i < 60:
        if this:
            that
        elif this:
            that other thing
        else:
            break
        i += 1
    if while loop has found the right thing:
        N += 1
    else:
        change conditions

这个 break 命令只会跳出第一个循环,所以我想知道如何简单地跳出两个循环。可能值得一提的是,所有这些都在另一个 for 循环中,我不想打破它。谢谢。

封装成一个函数,return完成后?

使用flag:

flag = True
while N <= 8:
    while i < 60:
        if this:
            that
        elif this:
            that other thing
        else:
            flag = False    # To exit from outer while loop
            break
        i += 1
    if(not flag):               
        break               # Condition in inner loop is met
    if while loop has found the right thing:
        N += 1
    else:
        change conditions

使用旗帜;这里使用trigger

trigger = False
while N <= 8:
    while i < 60:
        if this:
            that
        elif this:
            that other thing
        else:
            trigger = True
            break
        i += 1
    if trigger:
        break
    elif while loop has found the right thing:
        N += 1
    else:
        change conditions