Python learning-2 dictionary implementation level 3 menu,

Source: Internet
Author: User

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 ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.