作者:caiyafang4376 | 来源:互联网 | 2023-10-12 12:18
篇首语:本文由编程笔记#小编为大家整理,主要介绍了Python字典实现简单的三级菜单相关的知识,希望对你有一定的参考价值。
data = {
"北京":{
"昌平":{"沙河":["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 = Trueif choice_j == "b": breakelif 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: