Python learning-2 dictionary implementation level 3 menu,
Menu = {
"Jiangsu ":{
'Nanjing ': ["Jiangning", 'gulou', 'liuhe '],
'Nantong ': ["haimen", "Rugao", "Hai 'an"],
'Suzhou ': ["Kunshan", "wuzhong district", 'changshu']
},
"Anhui ":{
'Hefei ': ["luyang", "yaohai", "Bao he", "Shushan", "feidong county", "Feixi county", "Chaohu", "Changfeng ", "hi-tech", "binhu"],
'Shanghai': ["Jin 'an district", "Yu 'an district", "Ye Ji", "huoqiu", "Jin Zhai", "Huoshan", "Shu Cheng"]
},
'Shanghai ':{}
}
Cmenu = menu # set the current menu position
Pamenu = [] # Put the parent menu into a list, which is initially an empty list
While True:
For I in cmenu:
Print (I)
Choice = input ("input key/B: return, q: Exit :")
If choice in cmenu:
If not isinstance (cmenu, list): # judge whether the current menu is not a list
If cmenu [choice]: # if this item determines that the sub-layer of the menu is a dictionary, if the dictionary is not empty, for example, the sub-layer in Shanghai is an empty dictionary.
Pamenu. append (cmenu) # Add the current layer to the parent layer list
Cmenu = cmenu [choice] # change the current layer to the selected child Layer
Else: print ("This item is the last layer ")
Else: print ("This item is the last layer ")
Elif choice = 'B': # If B is selected
If pamenu = []: # if the parent layer is empty, the current layer is the top layer.
Cmenu = menu
Else: cmenu = pamenu. pop () # When the parent layer is not empty, select the last menu
Elif choice = 'q ':
Break
Else:
Print ('None Project ')