码迷,mamicode.com
首页 > 编程语言 > 详细

老男孩python3学习,课堂作业1.2 多级菜单查询

时间:2017-08-11 16:37:47      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:enum   auth   rate   python3   author   河北省   开平   子菜单   1.2   

# Version: python3.6
# Author: Gao
# 多级菜单:三级菜单,可依次选择进入子菜单,列表,字典

city_dict = {
"四川省": {
"成都市": ["锦江区", "青羊区", "金牛区", "武侯区", "成华区", "龙泉驿区"],
"攀枝花市": ["东区", "西区", "仁和区", "米易县", "盐边县"],
"泸州市": ["江阳区", "纳溪区", "龙马潭区", "泸县", "合江县"],
"德阳市": ["旌阳区", "中江县", "罗江县", "广汉市", "什邡市"]
},
"河北省": {
"石家庄市": ["长安区", "桥东区", "桥西区", "新华区", "井陉矿区"],
"唐山市": ["路南区", "路北区", "古冶区", "开平区", "丰南区"]
}
}
city_index = [(index, key) for index, key in enumerate(city_dict)] # 创建查询索引,enumerate用来遍历数据对象(这里字典表)
city_index.append((len(city_dict), ‘退出‘)) # 添加退出选项

while True:

print(‘_______三级城市查询_______‘)
print(‘__________________________‘)
for i in city_index: # for省份列表(0, ‘四川省‘), (1, ‘河北省‘), (2, ‘退出‘)
for j in i: # for (0, ‘四川省‘)、xxx、xxx
print(j, end=‘ ‘) # 打印省份
print(‘ ‘)
province_num = input("请输入你想查询的省份编号:")
province = city_index[int(province_num)][1] # 在city_index 里 按照输入的省份编号进行索引
if int(province_num) == int(len(city_dict)): # 选择退出执行
print("欢迎再次登录,再见!")
break
# print(province)
#print(len(city_dict[province])
while True:
c_index = [(index, key) for index, key in enumerate(city_dict[province])] # 同上原理
c_index.append((len(c_index), ‘返回‘))
for i in c_index:
for j in i:
print(j, end=‘ ‘)
print(‘‘)
c_num = input("请输入你想查询的城市编号:")
if int(c_num) == int(len(city_dict[province])):
break
city = c_index[int(c_num)][1]
county = list(enumerate(city_dict[province][city]))
print(county)
print(‘___________end_____________‘)

老男孩python3学习,课堂作业1.2 多级菜单查询

标签:enum   auth   rate   python3   author   河北省   开平   子菜单   1.2   

原文地址:http://www.cnblogs.com/hulu22/p/7346535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!