〖Demo〗-- 三级菜单 (基础版)

【三级菜单】

1. 打印省、市、县三级菜单
2. 可随时返回上一级
3. 可随时退出程序

  1 menu = {
  2     '北京':{
  3         '海淀':{
  4             '五道口':{
  5                 'soho':{},
  6                 '网易':{},
  7                 'google':{}
  8             },
  9             '中关村':{
 10                 '爱奇艺':{},
 11                 '汽车之家':{},
 12                 'youku':{},
 13             },
 14             '上地':{
 15                 '百度':{},
 16             },
 17         },
 18         '昌平':{
 19             '沙河':{
 20                 '老男孩':{},
 21                 '北航':{},
 22             },
 23             '天通苑':{},
 24             '回龙观':{},
 25         },
 26         '朝阳':{
 27             '大悦城':{
 28                 'GOP':{},
 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 running = False
 55 
 56 while not running:
 57     for key in menu:   #省层判断!将字典中对应的键(省份)输出
 58         print(key)
 59     choice = input("请输入省份:").strip()  #输入
 60     if len(choice)==0: continue   # 跳出本次循环,继续下一次循环
 61     if choice in menu:   #输入的在省城字典中
 62 
 63         while not running:#  市层判断
 64             next_layer = menu[choice]  #取出输入的省份,对应的值,并赋给新变量
 65             for key2 in next_layer:  #循环打印输出内容
 66                 print(key2)
 67             choice2 = input("请输入市:").strip()  #输入
 68             if len(choice2) == 0: continue
 69             if choice2 in next_layer:
 70 
 71                 while not running:    #地名判断!
 72                     next_layer_next = next_layer[choice2]
 73                     for key3 in next_layer_next:
 74                         print(key3)
 75                     choice3 = input("请输入地名:").strip()
 76                     if len(choice3) == 0: continue
 77                     if choice3 in next_layer_next:
 78 
 79                         while not running:   #查看地名下内容!
 80                             next_layer_next1 = next_layer_next[choice3]
 81                             for key4 in  next_layer_next1:
 82                                 print(key4)
 83                             choice4 = input("请输入:").strip()
 84                             if choice4 == "b": break
 85                             elif choice4 == "q": running = True
 86                             else:
 87                                 print("错误的输入")
 88                     elif choice3 == "b":
 89                         break
 90                     elif choice3 == "q":
 91                         running = True
 92                     else:
 93                         print("错误的输入")
 94             elif choice2 == "b":
 95                 break
 96             elif choice2 == "q":
 97                 running = True
 98             else:
 99                 print("错误的输入")
100     elif choice =="q" or choice == "b":
101         running = True
102     else:
103         print("!!错误的输入!!
---------------")
原文地址:https://www.cnblogs.com/SHENGXIN/p/7422073.html