多级菜单初写(dict使用)

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# name:zzyu
map = {
"中国":{
"北京":{
"昌平区":{
"沙河镇":["西沙屯","路庄","王庄"],
"阳坊镇":["东贯市","西贯市","阳坊"],
},
"密云区":{
"高岭镇":["四合村","燕落村","高岭村"],
"密云镇":["阳光小区","月光小区","星光小区"]
},
"朝阳区":{
"北苑":["北苑小区","茉莉园小区","丽春园小区"],
"霍营":["霍营地铁","望京地铁","北苑地铁"]
}
},
"河北":{
"河北省":{
"石家庄":["石家庄小区","二小区","三小区"],
"保定":["保一区","保二区","保三区"]
},
"唐山":{
"开平区":["大麻各庄村","茅草营村","粟园镇"],
"古冶区":["刘各庄村","小寨村","养马庄村"]
},
"廊坊":{
"安次区":["小屯村","青坨村","东北尹"],
"武清区":["新刘庄","前台","北寺"]
}
},
"天津":{
"北辰区":{
"一镇":["心罗园","紫罗园","心泽园"],
"二镇":["荣业园","荣熙园","荣翔园"]
},
"红桥区":{
"三镇":["水竹园","水木天成","华水大厦"],
"四镇":["运通园","顺通园","天阔园"]
},
"西青区":{
"五镇":["西清医院","西青政府","西青银行"],
"六镇":["双河新村","河畔人家","文化中心"]
}
}
}
}
print("\033[31;1m选择期间按<q>退出,按<b>返回上一层\033[0m")
exit_flag = False
while not exit_flag:
for i in map:
print(i)
user_choice = input("请选择您的国家:")
if user_choice in map:
while not exit_flag:
for j in map[user_choice]:
print(j)
user_choice_1 = input("请选择您的城市:")
if user_choice_1 in map[user_choice]:
while not exit_flag:
for p in map[user_choice][user_choice_1]:
print(p)
user_choice_2 = input("请选择您所在的区县:")
if user_choice_2 in map[user_choice][user_choice_1]:
while not exit_flag:
for k in map[user_choice][user_choice_1][user_choice_2]:
print(k)
user_choice_3 = input("请选择您所在的乡镇:")
if user_choice_3 in map[user_choice][user_choice_1][user_choice_2]:
for f in map[user_choice][user_choice_1][user_choice_2][user_choice_3]:
print(f)
user_choice_4 = input("\033[31;1m已到最后一层,请选择<b>或<q>:\033[0m")
if user_choice_4 == "q":
exit_flag = True
elif user_choice_4 == "b":
pass
elif user_choice_3 == "q":
exit_flag = True
elif user_choice_3 == "b":
break
else:
print("输入有误请重新输入.....")
elif user_choice_2 == "q":
exit_flag = True
elif user_choice_2 == "b":
break
else:
print("输入有误请重新输入.....")
elif user_choice_1 == "q":
exit_flag = True
elif user_choice_1 == "b":
break
else:
print("输入有误请重新输入.....")
elif user_choice == "b":
print("已返回,请重新输入。")
elif user_choice == "q":
print("再见")
break
else:
print("您输入的国家不存在,请重新输入.....")

猜你喜欢

转载自www.cnblogs.com/zzyuhehe/p/12725729.html
今日推荐