Python three-level menu instance,

Source: Internet
Author: User

Python three-level menu instance,

Requirements:

Print province, city, and county menus

Can return to the upper-level

Exit the program at any time

Version 1

# _ Author: Ahern Li # @ _ date: menu = {'zhejiang province ': {'hangzhou City': {'yuhang district ': {'zhongtai ':{}, 'fling' :{}, 'xihu district ': {'xihu' :{}, 'left' :{}}, 'wenzhou City ': {'cangnan ': {'lingxi' :{}, 'longgang ':{}}, 'ruian': {'anyang ':{}, 'jinhu ':{}}}}, 'guangdong province ': {'guangzhou City': {'yuexiu district ': {'renmin road' :{}, 'beijing': {}}, 'liwan district': {'sandup ': {}, 'longjin' :{}}, 'zhuhai City ': {'xiangzhou region': {'gongbei' :{}, 'geida ':{}}, 'Golden Bay Area ': {'hongqi Zhen' :{}, 'pingsha Zhen ':{}}}# flag q_flag = True # exit flag Q_flag = True # return, exit flag appears False exit loop while q_flag and Q_flag: # traverse Print province for I in menu: print (I) province = input ('enter the province to be searched (Q, exit ):'). strip () if province in menu: while q_flag and Q_flag: for I in menu [province]: print (I) city = input ('enter the city (q, return or Q, exit ):'). strip () if city in menu [province]: while q_flag and Q_flag: for I in menu [province] [city]: print (I) county = input ('Enter the region or county (q, return or Q, exit ):'). strip () if county in menu [province] [city]: while q_flag and Q_fla G: for I in menu [province] [city] [county]: print (I) # The last page is displayed. The input format is choice = input ('Last page! Q, return or Q, exit :'). strip () if choice = 'q': # With else: q_flag = True, exit the layer loop and return the upper loop q_flag = False elif choice = 'q ': # exit the Large Loop Q_flag = False else: # The system prompts that the input is invalid print ('input error! ') # Change the value of q_flag to exit a loop. else: q_flag = True elif county = 'q': q_flag = False 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: menu = {'zhejiang province ': {'hangzhou City': {'yuhang district ': {'zhongtai ':{}, 'fling' :{}, 'xihu district ': {'xihu' :{}, 'left' :{}}, 'wenzhou City ': {'cangnan ': {'lingxi' :{}, 'longgang ':{}}, 'ruian': {'anyang ':{}, 'jinhu ':{}}}}, 'guangdong province ': {'guangzhou City': {'yuexiu district ': {'renmin road' :{}, 'beijing': {}}, 'liwan district': {'sandup ': {}, 'longjin' :{}}, 'zhuhai City ': {'xiangzhou region': {'gongbei' :{}, 'geida ':{}}, 'Golden Bay Area ': {'hongqi Zhen' :{}, 'pingsha Zhen ':{}}}current_layer = menu # Implement Dynamic Loop parent_layer = [] # retain all parent layers, the last element is always the parent layer while True: print ()# Print (I) choice = input ('Enter the query address \ n >>> :'). strip () if choice in current_layer: if current_layer [choice]: # determine whether it is the last layer parent_layer.append (current_layer) # before entering the child layer, add the current layer as the parent layer current_layer = current_layer [choice] # modify the child layer else: print ('current is the last page') elif choice = '': continue elif choice = 'q': # Return the upper layer if parent_layer: # determine whether parent_layer is null current_layer = parent_lay Er. pop () # retrieve the parent layer of the current layer # exit loop elif choice = 'q': break else: print ('input error! ')

The above example of the Python three-level menu is all the content shared by xiaobian. I hope to give you a reference, and hope you can support the help house.

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.