Python之多级菜单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之多级菜单相关的知识,希望对你有一定的参考价值。
要求:
- 三级菜单
- 可依次选择进入各子菜单
流程图后续再补充
代码如下:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author Jiang 4 5 menu={ 6 ‘陕西‘:{ 7 ‘西安‘:[‘大雁塔‘,‘兵马俑‘], 8 ‘宝鸡‘:[‘法门寺‘,‘云台山‘], 9 ‘汉中‘:[‘青木川‘,‘武侯祠‘], 10 }, 11 ‘山西‘:{ 12 ‘太原‘:[‘晋祠‘,‘纯阳宫‘], 13 ‘大同‘:[‘恒山‘,‘华严寺‘], 14 ‘临汾‘:[‘壶口瀑布‘,‘千佛洞‘], 15 }, 16 ‘湖南‘:{ 17 ‘长沙‘:[‘岳麓山‘,‘橘子洲‘], 18 ‘株洲‘:[‘神农谷‘,‘炎帝陵‘], 19 ‘衡阳‘:[‘衡山‘,‘水帘洞‘], 20 }, 21 } 22 23 province_list=list(menu.keys()) 24 while True: 25 for index,key in enumerate(menu.keys(),1): 26 print(‘省‘,index,key) 27 pro_id=input(‘请输入省编号,或输入q退出:‘) 28 if pro_id.isdigit(): 29 pro_id=int(pro_id) 30 if pro_id>0 and pro_id<=len(province_list): 31 pro_name=province_list[pro_id-1] 32 city_list=list(menu[pro_name].keys()) 33 while True: 34 for j in city_list: 35 print(‘市‘,city_list.index(j)+1,j) 36 city_id=input(‘请输入市编号,或输入b返回上级菜单,或输入q退出:‘) 37 if city_id.isdigit(): 38 city_id=int(city_id) 39 if city_id>0 and city_id<=len(city_list): 40 city_name=city_list[city_id-1] 41 view_list=menu[pro_name][city_name] 42 while True: 43 for i in view_list: 44 print(‘景点‘,view_list.index(i)+1,i) 45 view_id=input(‘请输入景点编号,或输入b返回上级菜单,或输入q退出:‘) 46 if view_id==‘b‘: 47 break 48 elif view_id==‘q‘: 49 exit() 50 else: 51 print(‘输入不合格‘) 52 else: 53 print(‘您输入的用户%d不存在‘ %city_id) 54 elif city_id==‘b‘: 55 break 56 elif city_id==‘q‘: 57 exit() 58 else: 59 print(‘输入不合格‘) 60 else: 61 print(‘您输入的用户%d不存在‘ %pro_id) 62 elif pro_id==‘q‘: 63 exit() 64 else: 65 print(‘输入不合格‘)
以上是关于Python之多级菜单的主要内容,如果未能解决你的问题,请参考以下文章