Python Three-level menu instance detailed

Source: Internet
Author: User
Here is an example of a Python level three menu for everyone. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Requirements:

Print provincial, city, and county level three menus

Can return to the upper level

Can exit the program at any time

Version 1


# _author:ahern li# @_date:2017/9/12menu = {' Zhejiang province ': {' Hangzhou ': {' Yuhang ': {' Zhongtai ': {}, ' Pro flat ': {}}, ' West L. District ': {' West lake ': {}, ' left      ': {}}, ' Wenzhou ': {' Cangnan ': {' Ling Xi ': {}, ' Longgang ': {}}, ' Ruian County ': {' Anyang ': {}, ' Kam Lake ': {}}}, ' Guangdong Province ': {' Guangzhou ': { ' Yuexiu District ': {' Renmin road ': {}, ' Beijing Rd ': {}}, ' Liwan District ': {' Shamian ': {}, ' Long Jin ': {}}}, ' Zhuhai ': {' Xiangzhou District ': {' arch North ': {}, ' Jida ': {}}, ' King Bay District ': {' Red Banner(marxist-leninist) Town '  : {}, ' flat sand town ': {}}}}}# return token q_flag = true# exit Tag Q_flag = true# returned, exit token appears false exit loop while Q_flag and Q_flag: # Traverse Print Province       For I in Menu:print (i) Province = input (' Please enter the province to find (Q, exit): '). Strip () if province in Menu:while Q_flag and Q_flag: For I in Menu[province]: print (i) city = input (' Enter the municipality you want to find (q, return or Q, exit): '). Strip () if Urban in MENU[PR Ovince]: While Q_flag and q_flag:for I in menu[province][city]: print (i) County = INP              UT (' Please enter the region or county you want to find (q, return or Q, exit): '). Strip () if county in menu[province][city]: while Q_flag and Q_flag: For I in menu[Province][city][county]: Print (i) # hint last page, input format choice = input (' last page! Q, return or Q, exit : '). Strip () if choice = = ' Q ': # mate Else:q_flag = True to exit the layer loop, return to upper loop Q_flag = F                Alse elif Choice = = ' Q ': # exit Cycle Q_flag = False Else:            # Prompt input illegal print (' input error! ') # change back to Q_flag value, implement only exit one layer loop Else:q_flag = True elif County = = ' Q ': Q_flag = Fals        e elif County = = ' Q ': Q_flag = False else:print (' input error! ')      Else:q_flag = True elif City = = ' Q ': Q_flag = false elif City = = ' Q ': Q_flag = False    Else:print (' Input error! ') Else:q_flag = True elif province = = ' Q ': Q_flag = False else:print (' input error! ')

Version 2


# _author:ahern li# @_date:2017/9/12menu = {' Zhejiang province ': {' Hangzhou ': {' Yuhang ': {' Zhongtai ': {}, ' Pro flat ': {}}, ' West L. District ': {' West lake ': {}, ' left      ': {}}, ' Wenzhou ': {' Cangnan ': {' Ling Xi ': {}, ' Longgang ': {}}, ' Ruian County ': {' Anyang ': {}, ' Kam Lake ': {}}}, ' Guangdong Province ': {' Guangzhou ': { ' Yuexiu District ': {' Renmin road ': {}, ' Beijing Rd ': {}}, ' Liwan District ': {' Shamian ': {}, ' Long Jin ': {}}}, ' Zhuhai ': {' Xiangzhou District ': {' arch North ': {}, ' Jida ': {}}, ' King Bay District ': {' Red Banner(marxist-leninist) Town ' : {}, ' flat sand town ': {}}}}}current_layer = menu # implements dynamic loop Parent_layer = [] # preserves all parent layers, and the last element is always parent while True:print () # is only The print beauty for I in Current_layer: # Traverse Print address prints (i) choice = input (' Please enter the query address below \n>>>: '). Strip () if choice in C Urrent_layer:if Current_layer[choice]: # To determine if the last layer Parent_layer.append (current_layer) # Before entering the child layer, add the current layer as the parent layer Curre Nt_layer = Current_layer[choice] # Modify the sub-layer Else:print (' Currently the last page ') elif choice = = ': Continue elif choice = = ' Q ': # Return to Upper if Parent_layer: # to determine if Parent_layer is empty current_layer = Parent_layer.pop () # Remove current layer Parent # exit loop Elif Ch Oice = = ' Q ': Break ELse:print (' Input error! ') 
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.