三级菜单程序

要求: 

  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     },
29     '上海':{
30         '闵行':{
31             "人民广场":{
32                 '炸鸡店':{}
33             }
34         },
35         '闸北':{
36             '火车战':{
37                 '携程':{}
38             }
39         },
40         '浦东':{},
41     },
42     '山东':{},
43 }
44 
45 current_level = menu
46 last_level = []
47 
48 while True:
49     for key in current_level: #打印第一层
50         print(key)
51     choice = input(">>:").strip()  #输入你要进去的
52     if len(choice) == 0 :continue #输入的choice为空,则continue
53     if choice == "b": #输入b,退回上一次
54         if len(last_level) == 0:break #退到头了,在再退就退出了
55         current_level = last_level[-1] #-1代表最后一位
56         last_level.pop() #需要删除最后一位,才能持续后退
57     if choice not in current_level:continue #输入的choice不在字典中,则continue
58     last_level.append(current_level)  #记住当前层
59     current_level = current_level[choice] #进入下一层
原文地址:https://www.cnblogs.com/wangmo/p/5997988.html