python – 如何从内部循环中断到外部循环的开头

什么是从内循环中断的最好方法,所以我到达外循环的开始

while condition:
    while second_condition:
        if some_condition_here:
            get_to_the_beginning_of_first_loop

现在我有类似的东西

while condition:
    while second_condition:
        if condition1:
            break
    if condition1:
        continue

最佳答案 Python可以选择while循环的else:子句.如果你不调用break,则会调用它,因此它们是等效的:

while condition:
    while second_condition:
        if condition1:
            break
    if condition1:
        continue
    do_something_if_no_break()

和:

while condition:
    while second_condition:
        if condition1:
            break
    else:
        do_something_if_no_break()
点赞