python多重循环break_Python教程:跳出多层循环for、while

303222811d5612435198332931e4c234.png

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跳出多层循环

#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

跳出多层循环简化版

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值