Python教程:跳出多層循環for、while

for跳出多層循環python

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:
            break
    if break_flag: #if break_falg == True:
        print("我兒子死了,我也不活了..")
        break
print("keep going....")

while跳出多層循環app

#Python學習交流QQ羣:778463939
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....")

跳出多級菜單學習

map={
    "華南":{
        "廣東":["廣州市","佛山市","深圳市","東莞市"],
        "廣西":["南寧市","柳州市","桂林市","北海市"],
        "海南":["海口市","三亞市","三沙市","儋州市"]
             },
    "華東":{
        "上海":["黃浦區","盧灣區","徐彙區","長寧區"],
        "安徽":["合肥市","蕪湖市","淮南市","馬鞍山市"],
        "江蘇":["南京市","無錫市","徐州市","常州市"]
            }
}
flag = False
while flag == False:
    print("中國有以下地區:")
    for i in map.keys():
        print(i)
    area = input("請選擇一個地區、b或者q:").strip()
    if area == "b":
        break
    if area == "q":
        flag = True
    if len(area) == 0:
        continue
    if area in map.keys():
        while flag == False:
            print("該地區有以下省:")
            for j in (map[area]).keys():
                print(j)
            province = input("請選擇一個省、b或者q:").strip()
            if province == "b":
                break
            if province == "q":
                flag = True
            if len(province) == 0:
                continue
            if province in  (map[area]).keys():
                while flag == False:
                    print("該省有以下城市:")
                    for j in map[area][province]:
                        print(j)
                    city = input("請選擇b或者q:")
                    if city == "b":
                        break
                    if city == "q":
                        flag = True
                    if len(city) == 0:
                        continue

跳出多層循環簡化版google

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '網易':{},
                'google':{}
            },
            '中關村':{
                '愛奇藝':{},
                '汽車之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龍觀':{},
        },
        '朝陽':{},
        '東城':{},
    },
    '上海':{
        '閔行':{
            "人民廣場":{
                '炸雞店':{}
            }
        },
        '閘北':{
            '火車戰':{
                '攜程':{}
            }
        },
        '浦東':{},
    },
    '山東':{},
}
last_layers =  [  menu  ]  #上一層
current_layer = menu  #當前層
while True:
    for key in current_layer:
        print(key)
    choice = input(">>:").strip()
    if len(choice)==0:continue
    if choice in current_layer: #進入下一層
        last_layers.append(current_layer) #當前層添加到列表
        current_layer = current_layer[choice] #北京
    if choice == "b":
        if last_layers:
            current_layer = last_layers[-1] #取到上一層,賦值給current_layer
            last_layers.pop()
    if choice == 'q':
        break
相關文章
相關標籤/搜索