跳出多层循环forwhile

Posted 关灯吃面

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跳出多层循环forwhile相关的知识,希望对你有一定的参考价值。

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

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....")

 跳出多级菜单

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

 跳出多层循环简化版

 

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

 

以上是关于跳出多层循环forwhile的主要内容,如果未能解决你的问题,请参考以下文章

Java跳出多层循环

java的break跳出多层循环

python中forwhile循环if嵌套的使用

js 跳出循环

python基础2------跳出多层循环和购物车程序

PHP跳出多层循环