python多级菜单_python 三级菜单

1 #!/usr/bin/env python

2 #-*- coding: utf-8 -*-

3 #@Time : 2016/12/5 13:36

4 #@Author : zjz10

5 #@Site :

6 #@File : Three level menu.py

7 #@Software: PyCharm

8

9

10 #定义一级省市菜单的函数

11 importsys12 from termcolor importcolored, cprint13 #导入sys,termcolor,cprint模块(模块分别是:系统模块,颜色模块)

14

15 defmenu_1():16 #定义函数menu_1

17 print('省市级一级菜单'.center(40,'*'))18 for identifier1,key1 in enumerate(dic.keys(),1):19 print(identifier1,key1)20 #遍历字典dic的省市级一级菜单

21 dic_key[str(identifier1)] =key122 #将省市级一级菜单的key的下标和key放到dic_key的临时字典中

23 use = input('Please select the province level menu, enter the Q exit program, enter the B to return to the first level menu:')24 #提示用户选择一级菜单,输入q退出输入b返回上一级菜单

25 if use == 'q':26 quit("exit program")27 elif use == 'b':28 print('This is a menu that cannot be returned.')29 return

30 #判断输入的值,如果等于q就退出程序,如果等于b返回上一级菜单

31 elifdic_key.get(use,0):32 menu_2(dic_key[use])33 #判断用户输入的值,是否在临时的字典key中,如果有调用第二级菜单,否则提示输入错误,循环菜单

34 else:35 print(colored('您输入的有误,请重新输入'.center(40,'-'),'red'))36 print(colored('You entered is incorrect, please re-enter','red'))37

38

39

40 #定义市的第二级函数

41 defmenu_2(use1):42 #定义函数menu_2(use1)

43 whileTrue:44 #输入错误时,重新加载第二级菜单

45 print('')46 print('区级第二级菜单'.center(40,'*'))47 for identifier2,key2 in enumerate(dic[use1].keys(),1):48 print(identifier2,key2)49 #遍历dic字典的二级菜单

50 dic_key[str(identifier2)] =key251 #将二级菜单key的下标和key放到dic_key的临时字典

52 use2 = input('Please select the municipal menu, enter the Q exit program, enter the B to return to the first level menu:')53 #提示用户选择二级菜单,输入q退出输入b返回上一级菜单

54 if use2 == 'q':55 quit('exit progarm')56 elif use2 == 'b':57 menu_1()58 #判断输入的值,如果等于q就退出程序,如果等于b返回上一级菜单

59 elifdic_key.get(use2,0):60 menu_3(use1,dic_key[use2])61 #判断用户输入的值,是否在临时的字典key中,如果有调用第三级菜单,否则提示输入错误

62 else:63 print(colored('您输入的有误,请重新输入'.center(40, '-'), 'red'))64 print(colored('You entered is incorrect, please re-enter', 'red'))65

66 #定义区县三级菜单的函数

67 defmenu_3(use1,use2):68 #定义函数menu_2(use1)

69 whileTrue:70 #输入错误时,重新加载第二级菜单

71 print('')72 print('县三级菜单'.center(40,'*'))73 for identifier3,key3 in enumerate(dic[use1][use2],1):74 print(identifier3,key3)75 #遍历dic字典的二级菜单

76 dic_key[str(identifier3)] =key377 #将二级菜单key的下标和key放到dic_key的临时字典

78 use3 = input('Please select the district level menu,enter the Q exit program, enter the B to return to the first level menu:')79 #提示用户选择二级菜单,输入q退出输入b返回上一级菜单

80 if use3 == 'q':81 quit('exit progarm')82 elif use3 == 'b':83 menu_2(use1)84 #判断输入的值,如果等于q就退出程序,如果等于b返回上一级菜单,否则提示输入错误

85 else:86 print(colored('您输入错误,请重新输入'.center(40, '-'), 'red'))87 print(colored('You entered is incorrect, please re-enter', 'red'))88

89 if __name__ == '__main__':90 dic ={91 '北京市': {92 '市辖区': ['东城区', '西城区', '朝阳区', '丰台区', '石景山区', '海淀区', '门头沟区', '房山区', '通州区', '顺义区', '昌平区', '大兴区', '怀柔区', '平谷区'],93 '县': ['密云县', '延庆县']94 },95 '天津市': {96 '市辖区': ['和平区', '河东区', '河西区', '南开区', '河北区', '红桥区', '东丽区', '西青区', '津南区', '北辰区', '武清区', '宝坻区', '滨海新区', '宁河区',97 '静海区'],98 '县': ['蓟县']99 },100 '石家庄市': {101 '区': ['市辖区', '长安区', '桥西区', '新华区', '井陉矿区', '裕华区', '藁城区', '鹿泉区', '栾城区'],102 '县': ['井陉县', '正定县', '行唐县', '灵寿县', '高邑县', '深泽县', '赞皇县', '平山县', '元氏县', '赵县'],103 '市':['晋州市', '新乐市']104 },105 '唐山市': {106 '区': ['市辖区', '路南区', '路北区', '古冶区', '开平区', '丰南区', '丰润区', '曹妃甸区'],107 '县': ['滦县', '滦南县', '乐亭县', '迁西县', '玉田县'],108 '市': ['遵化市', '迁安市']109 }110 }111

112 dic_key ={}113 whileTrue:114 menu_1()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值