加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:03:57 所属栏目: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 con
什么是从内循环中断的最好方法,所以我到达外循环的开始

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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读