[terry笔记]python三级菜单

把三级菜单输出,选择后逐层显示,”b“返回上一级菜单。

 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 }

 

二逼青年版:
 1 while True:
 2     for key in menu:
 3         print(key)
 4     choice = input(">>>:")
 5     if len(choice) == 0: continue
 6     if choice not in menu:continue
 7     while True:
 8         for key2 in menu[choice]:
 9             print(key2)
10         choice2 = input(">>>:")
11         if len(choice2) == 0: continue
12         if choice2 == "b": break
13         if choice2 not in menu[choice]: continue
14         while True:
15             for key3 in menu[choice][choice2]:
16                 print(key3)
17             choice3 = input(">>>:")
18             if len(choice3) == 0: continue
19             if choice3 == "b": break
20             if choice3 not in menu[choice][choice2]: continue
21             while True:
22                 for key4 in menu[choice][choice2][choice3]:
23                     print(key4)
24                 choice4 = input(">>>:")
25                 if len(choice4) == 0: continue
26                 if choice4 == "b": break
27                 if choice4 not in menu[choice][choice2][choice3]: continue

 

文艺青年版:

 1 current_level = menu
 2 last_level = []
 3 while True:
 4     for key in current_level:
 5         print(key)
 6     choice = input(">>>:")
 7     if len(choice) == 0:continue
 8     if choice == "b":
 9         if not last_level : break
10         current_level = last_level[-1]
11         last_level.pop()
12     if choice not in current_level:continue
13     last_level.append(current_level)
14     current_level = current_level[choice]

 

posted @ 2016-10-28 16:15  DoubleGinger  阅读(349)  评论(1编辑  收藏  举报