循环用的多,以后再优化吧
三级菜单:
- 运行程序输出第一级菜单
- 选择一级菜单某项,输出二级菜单,同理输出三级菜单
- 菜单数据保存在文件中
- 让用户选择是否要退出
- 有返回上一级菜单的功能
data.txt 保存数据文件
{'河北':{ "石家庄":{"长安区":["oldboy","baidu","tencent"],"桥东区":["百事可乐","雪碧","芬达"]}, "邯郸":{ "丛台区":["新世纪","天鸿"]}},
'北京':{"西城区":{"宣武门":["锦江之星","汉庭","如家"],"广安门":["希尔顿","速8","7天"]}}}
menu.py 运行文件
#!/use/bin/env python
# -*- coding:utf-8 -*-
f = open("data.txt","r",encoding="utf8")
f=f.read()
data=eval(f) # 把文本中内容转成字典
while True:
for line in data:
print(line)
choice = input("选择进入1:")
if choice in data:
while True:
for line2 in data[choice]:
print("-->",line2)
choice2 = input("选择进入2:")
if choice2 in data[choice]:
while True:
for line3 in data[choice][choice2]:
print("---->",line3)
choice3 = input("选择进入3:")
if choice3 in data[choice][choice2]:
for line4 in data[choice][choice2][choice3]:
print(line4)
choice4 = input("最后一层,返回请按b,退出请按q:")
if choice4 == "b":
pass
elif choice4 == "q":
exit()
elif choice3 == "b":
break
elif choice3 == "q":
exit()
elif choice2 == "b":
break
elif choice2 == "q":
exit()