三级菜单

 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_layer = menu
46 layers = []     #保存每次进入后的值
47 while 1:
48     for k in current_layer:
49         print(k)
50     choice = input(">").strip()
51     if not choice: continue
52     if choice in current_layer:
53         layers.append(current_layer)    #把当前层存到 layers .然后可以进下一次
54         current_layer = current_layer[choice]
55     elif choice == 'b':
56         if len(layers) != 0:
57             current_layer = layers.pop()
58         else:
59             print('顶层了')
60     else:
61         if choice == 'q':
62             print('bye ')
63             exit()

 

原文地址:https://www.cnblogs.com/666sss/p/11598124.html