Python3学习之路~2.5 简单的三级菜单程序

时间:2021-03-06 19:25:37

程序:
三级菜单

需求:

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

代码1:

data={
"山东":{
"济南":["历下区","高新区","长清区"],
"日照":["东港区","五莲县","岚山区"],
"青岛":["崂山区","黄岛区","市中区"]
},
"陕西": {
"西安": ["雁塔区", "高新区", "未央区"],
"延安": ["宝塔区", "宜川县", "黄陵县"],
"宝鸡": ["陈仓区", "岐山县", "太白县"]
}
} while True:
for i in data:
print(i)
choice = input("请选择进入:")
if choice in data:
while True:
for i2 in data[choice]:
print('\t',i2)
choice2 = input("请选择进入:")
if choice2 in data[choice]:
while True:
for i3 in data[choice][choice2]:
print('\t\t',i3)
choice3=input("最后一级,请输入q退出或输入b返回上一级:")
if choice3 == 'b':
break
elif choice3 == 'q':
exit()
else:
print("无效输入!")
elif choice2 == 'b':
break
elif choice2 == 'q':
exit()
else:
print("无效输入!")
elif choice == 'q':
exit()
else:
print("无效输入!")

代码2:

menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
} exit_flag = False
current_layer = menu layers = [menu] while not exit_flag:
for k in current_layer:
print(k)
choice = input(">>:").strip()
if choice == "b":
current_layer = layers[-1]
#print("change to laster", current_layer)
layers.pop()
elif choice not in current_layer:continue
else:
layers.append(current_layer)
current_layer = current_layer[choice]