Python implementation level Three menu

Source: Internet
Author: User

Requirements:    You can choose to go to each submenu    from any level back to the previous layer    you can exit the program from any level    required new knowledge points: List, dictionary
Use only one while loop

1 #!-*-coding:utf-8-*-2 3menu = {4     'Beijing': {5         'Haidian': {6             'Five crossing': {7                 'Soho': {},8                 'NetEase': {},9                 'Google': {}Ten             }, One             'Zhongguancun': { A                 'Iqiyi Art': {}, -                 'Autohome': {}, -                 'Youku': {}, the             }, -             'on the ground': { -                 'Baidu': {}, -             }, +         }, -         'changping': { +             'Shahe': { A                 'old boy': {}, at                 'Beihang University': {}, -             }, -             'Tin Tong Court': {}, -             'Huilongguan': {}, -         }, -         'Chaoyang': {}, in         'Dongcheng': {}, -     }, to     'Shanghai': { +         'Minhang': { -             "People's square": { the                 'Fried Chicken Shop': {} *             } $         },Panax Notoginseng         'Zhabei': { -             'Train Warfare': { the                 'Ctrip': {} +             } A         }, the         'Pudong': {}, +     }, -     'Shandong': {}, $ } $Current_layer = Menu#Implementing Dynamic Loops -Parent_layer = []#Keep all parent layers, and the last element is always the parent -  the  whileTrue: -     Print('-'* 10,"Menu",'-'* 10)Wuyi      forIinchCurrent_layer:#traverse the print address the         Print(i) -     Print("Please enter the menu name below, or B: Return to the previous layer, Q: Exit \ n",'-'* 26) WuChoice = input (">>>"). Strip () -     ifChoiceinchCurrent_layer: About         ifCurrent_layer[choice]:#determine if the final layer $Parent_layer.append (Current_layer)#Add the current layer as the parent layer before entering the child layer -Current_layer = Current_layer[choice]#Modify a child layer -         Else: -             Print('current is the last page') A     elifChoice = ="': +         Continue the     elifChoice = ='b' orChoice = ='B':#back to top -         ifParent_layer:#determine if the Parent_layer is empty $Current_layer = Parent_layer.pop ()#Remove the current layer parent layer the     #Exit Loop the     elifChoice = ='Q' orChoice = ='Q': the          Break the     Else: -         Print("\033[34;1m input Error, please re-enter \033[0m")


Python implementation level Three menu

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.