首页 > 代码库 > Python字典实现简单的三级菜单
Python字典实现简单的三级菜单
data = http://www.mamicode.com/{
"北京":{
"昌平":{"沙河":["oldboy","test"],"天通苑":["链接地产","我爱我家"]},
"朝阳":{"望京":["奔驰","陌陌"],"国贸":["CICC","HP"],"东直门":["advent","飞信"]},
"海淀":{}
},
"山东":{
"德州":{},
"青岛":{},
"济南":{}
},
"广东":{
"东莞":{},
"常熟":{},
"佛山":{}
}
}
exit_flag = False
while not exit_flag:
for i in data:
print(i)
choice_s = input("请输入以上任一省或直辖市名称>>>:")
if choice_s in data:
while not exit_flag:
for i in data[choice_s]:
print("\t",i)
choice_q = input("请输入以上任一市区名称>>>:")
if choice_q in data[choice_s]:
while not exit_flag:
for i in data[choice_s][choice_q]:
print("\t\t",i)
choice_j = input("请输入以上任一街道名称>>>:")
if choice_j in data[choice_s][choice_q]:
for i in data[choice_s][choice_q][choice_j]:
print("\t\t",i)
last = input("最后一层,按b返回>>>:")
if last == "b":
pass
elif last == "q":
exit_flag = True
if choice_j == "b":
break
elif choice_j == "q":
exit_flag = True
if choice_q == "b":
break
elif choice_q == "q":
exit_flag = True
要点:
1、字典的层级关系
2、break是终止本次循环,pass是什么也不做,一个占位符而已
3、巧妙使用exit_flag = False
while not exit_flag:
Python字典实现简单的三级菜单
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。