Three menu operation - recursion stack

the MENU = {
     ' Beijing ' : {
         ' Haidian ' : {
             ' Wudaokou ' : {
                 ' SOHO ' : {},
                 ' NetEase ' : {},
                 ' Google ' : {} 
            }, 
            ' Zhongguancun ' : {
                 ' iQIYI ' : {},
                 ' car home ' : {},
                 ' the youku ' : {} 
            },
            'On the ground ' : {
                 ' Baidu ' : {} 
            } 
        }, 
        ' Changping ' : {
             ' River ' : {
                 ' Old Boy ' : {},
                 ' Northern ' : {} 
            }, 
            ' Tiantongyuan ' : {},
             ' Huilongguan ' : {} 
        }, 
        ' sun ' : {},
         ' East ' : {} 
    }, 
    'Shanghai ': {
         " Minhang " : {
             " People's Square " : {
                 ' Fried Chicken ' : {} 
            } 
        }, 
        ' Zhabei ' : {
             ' Train war ' : {
                 ' Ctrip ' : {} 
            } 
        }, 
        ' Pudong ' : {} , 
    }, 
    ' Shandong ' : {} 
}
# Recursive
 1 def three(dic):
 2     while 1:
 3         for i in dic:
 4             print(i)
 5         key = input('input >>>').strip()
 6         if key == 'b' or key == 'q':
 7             return key
 8         elif key in dic.keys() and dic[key]:
 9             ret = three(dic[key])
10             if== key ' Q ' :
 . 11                  return  ' Q ' 
12 is          elif ( Not dic.get (key)) or ( Not DIC [key]):    # If the key is not input in the current dictionary, or the current value corresponding to the key is null 
13 is              Continue 
14  
15 Three (MENU)
# Stack implementation
 1 l = [menu]
 2 while l:
 3     for key in l[-1]:
 4         print(key)
 5     k = input('input>>>').strip()
 6     if k in l[-1].keys() and l[-1][k]:
 7         l.append(l[-1][k])
 8     if k == 'b':
 9         l.pop()
10     if k == 'q':
11         break
#
#

Guess you like

Origin www.cnblogs.com/Ryan-Fei/p/12214222.html