Python开发【程序】:三级菜单程序

# 需求:
# 可依次选择进入各子菜单
# 可从任意一层往回退到上一层
# 可从任意一层退出程序
# 所需新知识点:列表、字典数据结构:

一.low版:

  1 #思路:
  2 #使用列表来存放字典的每一层数据,根据索引来选择
  3 #后退到上一层可以通过一个while True 循环来控制,break跳出后退
  4 
  5 menu = {
  6     '北京':{
  7         '海淀':{
  8             '五道口':{
  9                 'soho':{},
 10                 '网易':{},
 11                 'google':{}
 12             },
 13             '中关村':{
 14                 '爱奇艺':{},
 15                 '汽车之家':{},
 16                 'youku':{},
 17             },
 18             '上地':{
 19                 '百度':{},
 20             },
 21         },
 22         '昌平':{
 23             '沙河':{
 24                 '老男孩':{},
 25                 '北航':{},
 26             },
 27             '天通苑':{},
 28             '回龙观':{},
 29         },
 30         '朝阳':{},
 31         '东城':{},
 32     },
 33     '上海':{
 34         '闵行':{
 35             "人民广场":{
 36                 '炸鸡店':{}
 37             }
 38         },
 39         '闸北':{
 40             '火车战':{
 41                 '携程':{}
 42             }
 43         },
 44         '浦东':{},
 45     },
 46     '山东':{},
 47 }
 48 
 49 province_list = list(menu.keys())   #省名称列表
 50 
 51 while True:
 52     print("".center(24,'*'))
 53     for i in province_list:
 54         print(province_list.index(i)+1,i)   #打印出各个省
 55     province_choice = input("请输入您所需要选择的省的编号,或者退出(q):")
 56     if province_choice.isdigit():    #判断是否为数字
 57         province_choice = int(province_choice)   #强制转换为数字
 58         if province_choice > 0 and province_choice <= len(province_list):  #判断输入的数值是否正确
 59             province_name = province_list[province_choice-1]   #取出选择的省的名称
 60             city_list = list(menu[province_name].keys())  #市名称列表
 61             while True:
 62                 print("".center(24,'*'))
 63                 for j in city_list:
 64                     print(city_list.index(j)+1,j)
 65                 city_choice = input("请输入您所选择的市的编号,返回(r),或者退出(q):")
 66                 if city_choice.isdigit():
 67                     city_choice = int(city_choice)
 68                     if city_choice > 0 and city_choice <= len(city_list):
 69                         city_name = city_list[city_choice-1]
 70                         country_list = list(menu[province_name][city_name].keys())
 71                         while True:
 72                             print("".center(24,'*'))
 73                             for k in country_list:
 74                                 print(country_list.index(k)+1,k)
 75                             country_choice = input("请输入您所选择的县的编号,返回(r),退出(q):")
 76                             if country_choice.isdigit():
 77                                 country_choice = int(country_choice)
 78                                 if country_choice > 0 and country_choice <= len(country_list):
 79                                     country_name = country_list[country_choice-1]
 80                                     company_list = list(menu[province_name][city_name][country_name].keys())
 81                                     while True:
 82                                         print("公司".center(24,'*'))
 83                                         for l in company_list:
 84                                             print(company_list.index(l)+1,l)
 85                                         company_choice = input("返回(r),退出(q):")
 86                                         if  company_choice == 'r':
 87                                             break
 88                                         elif company_choice == 'q':
 89                                             exit()
 90                                         else:
 91                                             print("您的输入%s有误!" % (company_choice))
 92                                 else:
 93                                     print("输入编号%s超限!" % (country_choice))
 94                             elif country_choice == 'r':
 95                                 break
 96                             elif country_choice == 'q':
 97                                 exit()
 98                             else:
 99                                 print("您的输入%有误!" % (country_choice))
100                     else:
101                         print("您输入的编号%s超限!" % (city_choice))
102                 elif city_choice == 'r':
103                     break
104                 elif city_choice == 'q':
105                     exit()
106                 else:
107                     print("您的输入%s有误!" % (city_choice))
108         else:
109             print("您的编号%s输入超限!" % (province_choice))
110     elif province_choice == 'r':
111         break
112     elif province_choice == 'q':
113         exit()
114     else:
115         print("您的输入%s有误!" % (province_choice))

输出结果终端展示:

 

缺点:重复代码太多

 

 二.高级版:

 

 1 #思路:
 2 #1.low版代码有好多重复的代码,设置一个动态的变量来控制
 3 menu = {...}
 4 current_layer = menu   #当前层
 5 # last_layer = menu
 6 layers = []   #
 7 while True:
 8     for k in current_layer:
 9         print(k)
10     choice = input(">:").strip()
11     if  not choice:continue
12     if choice in current_layer:
13         layers.append(current_layer)   #进入下一层,保存当前层到列表中
14         current_layer = current_layer[choice]   #动态的将下一层变成当前层,然后进行新的循环
15     elif choice == 'b':
16         if len(layers) != 0:   #判断列表是否为空
17             current_layer = layers.pop()  #将列表最后一个元素取出,并删除掉
18             # current_layer = layers[-1]
19             # del layers[-1]
20         else:
21             print("到达顶层!")
22     elif choice == 'q':
23         exit()
24     else:
25         print("输入有误")

 

输出结果终端展示:

缺点:layers这个列表会存放大量的数据,占用太多空间,后续继续优化

posted @ 2018-03-14 21:42  mumupa0824  阅读(804)  评论(0编辑  收藏  举报