1 three-level menu

 

. 1 MENU = {
 2      ' Beijing ' : {
 3          ' Haidian ' : {
 4              ' Wudaokou ' : {
 . 5                  ' SOHO ' : {},
 . 6                  ' YORK ' : {},
 . 7                  ' Google ' : {}
 . 8              },
 . 9              ' Zhongguancun ' : {
 10                  ' iQIYI ' : {},
 11                  ' car home ' : {},
 12                  'Youku ': {},
 13              },
 14              ' to a ' : {
 15                  ' Baidu ' : {}
 16              }
 17          }
 18          ' Changping ' : {
 19              ' River ' : {
 20                  ' old boys ' : {},
 21                  ' Northern ' : {}
 22              }
 23              ' Tiantongyuan ' : {},
 24              ' huilongguan ' : {}
 25          }
 26         ' Sun ' : {},
 27          ' East ' : {}
 28      }
 29      ' Shanghai ' : {
 30          ' minhang ' : {
 31              , " People's Square " : {
 32                  ' Fried Chicken ' : {}
 33              }
 34          } ,
 35          ' Zhabei ' : {
 36              ' train war ' : {
 37                  ' Cheng ' : {}
 38              }
 39         },
 40          ' Pudong ' : {}
 41      }
 42      ' Shandong ' : {}
 43  }
 44  # implement a click function: 
45  # Display [Beijing, Shanghai, Shandong], user input selecting 
46  # If the input [Beijing ], displays [Haidian, Changping, Chaoyang, East], users can opt to return to the layer, can be input selection 
47  # If the input Haidian [], displays [wudaokou, Zhongguancun] on the ground, the user can exit, return to the previous, also the input selection 
48  # If the input wudaokou [], displays [Sohu, Netease, Google], users can opt to return to the level, may be input selection
View Code

 

 1 # 递归思想
 2 def menu_func(menu):
 3     while True:
 4         for key in menu:
 5             print(key)
 6         inp = input('>>>').strip()
 7         if inp.upper() == 'Q': return 'q'
 8         if inp.upper() == 'B': return 'b'
 9         elif menu.get(inp):
10             flag = menu_func(menu[inp])
11             if flag == 'q': return 'q'
12 menu_func(menu)
13 print('已退出')
View Code

 

1  # Stack Thought 
2 LST = [MENU]
 . 3  the while LST:
 . 4      for Key in LST [-1 ]:
 . 5          Print (Key)
 . 6      InP = INPUT ( ' >>> ' )   
 . 7      IF inp.upper () == ' Q ' :
 . 8          BREAK 
. 9      elif inp.upper () == ' B ' :   # back to the previous 
10          lst.pop ()    # last element pop default deletion list 
. 11      elif LST [-1].get(inp):
12         lst.append(lst[-1][inp])
View Code

 

Guess you like

Origin www.cnblogs.com/liuweida/p/11099955.html