1. 程式人生 > >Python字典實現簡單的三級菜單

Python字典實現簡單的三級菜單

直轄市 test 什麽 bre python字典 山東 oldboy als 實現

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 = 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字典實現簡單的三級菜單