zoukankan      html  css  js  c++  java
  • 通过标志位跳出多层循环

    break只能跳出本层循环,若要跳出多层循环,可定义个标志位break_flag

    跳出多层for循环:

    break_flag=False
    for i in range(10):
        print("爷爷层",i)
        for j in range(10):
            print("爸爸层",j)
            if j == 3:
                break_flag=True
                break
            for k in range(10):
                print("孙子层",k)
                if k ==2:
                    break_flag=True
                    break
            if break_flag:
                print("儿子蒙了,不活了...")
                break
        if break_flag:
            print("儿子蒙了,不活了...")
            break

    跳出while多层循环:

    break_flag=False
    count=0
    while break_flag==False:
        print("爷爷层...")
        while break_flag==False:
            print("爸爸层...")
            while break_flag==False:
                count+=1
                if count>10:
                    break_flag=True
                print("孙子层...")
    
    print("keep going...")
  • 相关阅读:
    软链接
    yum
    vm
    tengine
    创智LIUNX
    作业11
    作业10
    作业9
    作业8
    作业7
  • 原文地址:https://www.cnblogs.com/stin/p/7857639.html
Copyright © 2011-2022 走看看