三级菜单的程序实现

  1 #三级菜单的实现
  2 #将字典类型的数据存储到文本文件当中 str()
  3 #将文本文件当中的数据转化为原来的类型 eval()
  4 
  5 # 程序: 三级菜单
  6 # 要求:
  7 # 打印省、市、县三级菜单
  8 # 可返回上一级
  9 # 可随时退出程序
 10 menu = {
 11     '北京':{
 12         '海淀':{
 13             '五道口':{
 14                 'soho':{},
 15                 '网易':{},
 16                 'google':{}
 17             },
 18             '中关村':{
 19                 '爱奇艺':{},
 20                 '汽车之家':{},
 21                 'youku':{},
 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         '浦东':{},
 50     },
 51     '山东':{},
 52 }
 53 
 54 #三级菜单,输入省份,显示市区,输入市区显示县,还可以在任意一层实现程序的回退
 55 #可以在每一层返回上一层
 56 #也可以在每一层进行退出
 57 while_flag = False
 58 #定义退出的表示位置:
 59 quit_flag = False
 60 while not while_flag and not quit_flag:
 61     for key in menu:
 62         print(key)
 63     choice = input(">>1 ").strip()
 64     if choice == "b":
 65         while_flag = True
 66     if choice =="q":
 67         quit_flag = True
 68         break #break之后就程序不会走else语句了
 69     if choice in menu:
 70         while not while_flag and not quit_flag:
 71             for key2 in menu[choice]:
 72                 print(key2)
 73             choice2 = input(">>2 ").strip()
 74             if choice2 == "b":
 75                 while_flag = True
 76             if choice2 =="q":
 77                 quit_flag = True
 78                 break #break之后就程序不会走else语句了
 79             if choice2 in menu[choice]:
 80                 while not while_flag and not quit_flag:
 81                     for key3 in menu[choice][choice2]:
 82                         print(key3)
 83                     choice3 = input(">>3 ").strip()
 84                     if choice3 == "b":
 85                         while_flag = True
 86                     if choice3 =="q":
 87                         quit_flag = True
 88                         break #break之后就程序不会走else语句了
 89                     if choice3 in menu[choice][choice2]:
 90                         while not while_flag and not quit_flag:
 91                             for key4 in menu[choice][choice2][choice3]:
 92                                 print(key4)
 93                             print("last level")
 94                             choice4 = input(">>4 ").strip()
 95                             if choice4 == "b":
 96                                 while_flag = True
 97                             if choice4 =="q":
 98                                 quit_flag = True
 99                                 break #break之后就程序不会走else语句了
100                         else:#上述语句当中没有break就会执行else循环的
101                              while_flag = False
102                 else:#上述语句当中没有break就会执行else循环的
103                      while_flag = False
104         else:#上述语句当中没有break就会执行else循环的
105              while_flag = False
106 else:#上述语句当中没有break就会执行else循环的
107     while_flag = False
108 #开始解决回退的问题
109 #思路是什么样子的:需要个标示位退出当前的while循环
110 
111 #现在开始解决程序的退出问题
112 #需要单独一个标示位表示程序的退出
113 #注意while break else的巧用
 1 #三级菜单的高级版实现:之前的实现有很多重复代码我们需要提取
 2 #思路 我们只需要每次遍历当前列表下的值,当前字典设备变量一直给赋值
 3 #定义当前的字典 初始值为menu
 4 current_layer = menu
 5 parent_layer =[]
 6 #遍历当前的字典
 7 #退回上级的实现 思路 定义个变量,每次将父级保存在变量里面
 8 while True:
 9     for key in current_layer:
10         print(key)
11     choice = input(">> ").strip()
12     if len(choice) == 0:continue
13     if choice in current_layer:
14         parent_layer.append(current_layer)#保存父级
15         current_layer = current_layer[choice]
16     elif choice == "b":
17         if parent_layer:
18             current_layer = parent_layer.pop()
19     elif choice == "q":
20         break
21     else:
22         print("无此项")

猜你喜欢

转载自www.cnblogs.com/neilyoung22/p/9191927.html
今日推荐