python 三级菜单

要求:

  1、编写三级菜单代码

  2、实现能够选择性的进入各个菜单

Readme:

  1、首先定义3级菜单的函数,使用def

  2、使用for遍历字典中的1,2,3级菜单的key,用enumerate的方法加上下标放到临时的空字典中

  3、input提示用户选择输入或者输入q退出b返回上一级菜单

  4、使用if判断语句对用户的输入进行判断

  5、如果用户输入的在临时字典中调用下一级菜单

  6、if __name__ == "__main__":

        定义三级菜单dic字典{}

 

流程图:

 

 

 

 

 

代码:

  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 import sys
 12 from termcolor import colored, cprint
 13 #导入sys,termcolor,cprint模块(模块分别是:系统模块,颜色模块)
 14 
 15 def menu_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)] = key1
 22         #将省市级一级菜单的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     elif dic_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 def menu_2(use1):
 42     #定义函数menu_2(use1)
 43     while True:
 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)] = key2
 51         #将二级菜单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         elif dic_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 def menu_3(use1,use2):
 68     # 定义函数menu_2(use1)
 69     while True:
 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)] = key3
 77             # 将二级菜单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     while True:
114         menu_1()
View Code

 

posted @ 2017-01-13 15:34  Brian_Zhu  阅读(642)  评论(0)    收藏  举报