三级菜单

三级菜单
打印省、市、县三级菜单
可随时返回上一级
可随时退出程序

流程图

代码如下:

menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{
'大悦城':{
'GOP':{},
'电影院':{},
},
"望京":{},
"团结湖":{},
},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{
"德州":{},
"青岛":{},
},
}
running = False

while not running:
for key in menu: #省层判断!将字典中对应的键(省份)输出
print(key)
choice = input("请输入省份:").strip() #输入
if len(choice)==0: continue # 跳出本次循环,继续下一次循环
if choice in menu: #输入的在省城字典中

while not running:# 市层判断
next_layer = menu[choice] #取出输入的省份,对应的值,并赋给新变量
for key2 in next_layer: #循环打印输出内容
print(key2)
choice2 = input("请输入市:").strip() #输入
if len(choice2) == 0: continue
if choice2 in next_layer:

while not running: #地名判断!
next_layer_next = next_layer[choice2]
for key3 in next_layer_next:
print(key3)
choice3 = input("请输入地名:").strip()
if len(choice3) == 0: continue
if choice3 in next_layer_next:

while not running: #查看地名下内容!
next_layer_next1 = next_layer_next[choice3]
for key4 in next_layer_next1:
print(key4)
choice4 = input("请输入:").strip()
if choice4 == "b": break
elif choice4 == "q": running = True
else:
print("错误的输入")
elif choice3 == "b":
break
elif choice3 == "q":
running = True
else:
print("错误的输入")
elif choice2 == "b":
break
elif choice2 == "q":
running = True
else:
print("错误的输入")
elif choice =="q" or choice == "b":
running = True
else:
print("!!错误的输入!! ---------------")

原文地址:https://www.cnblogs.com/zh605929205/p/6647093.html