Python Level 3 menu,

Source: Internet
Author: User

Python Level 3 menu,

Requirements:

Version 1

1 # _ author: Ahern Li 2 # @ _ date: 3 4 menu = {5 'zhejiang province ': {6 'hangzhou City': {7 'yuhang district ': {'zhongta' :{}, 'linping' :{}}, 8 'xihu district ': {'xihu' :{}, 'left' :{}} 9 }, 10 'wenzhou City ': {11 'cangnan County': {'lingxi' :{}, 'longgang ':{}, 12 'ruian County': {'anyang ':{}, 'jinhu ': {}}13} 14}, 15 'guangdong province': {16 'guangzhou City ': {17 'yuexiu district': {'renmin road ':{}, 'beijing': {}, 18 'liwan district ': {'sandup' :{}, 'longjin' :{}} 19}, 20 'zhuhai City ': {21 'xiangzhou district ': {'gongbei': {}, 'geida ':{}}, 22 'jinwan district': {'hongqi town ':{}, 'pingsha town ': {} 23} 24} 25} 26 27 # Return tag 28 q_flag = True29 # exit tag Note 30 Q_flag = True31 # Return, exit flag appears False exit loop 32 while q_flag and Q_flag: 33 # traverse print province 34 for I in menu: 35 print (I) 36 province = input ('Enter the province to search (Q, exit ):'). strip () 37 if province in menu: 38 while q_flag and Q_flag: 39 for I in menu [province]: 40 print (I) 41 city = input ('Enter the city you want to search for (q, return or Q, exit ):'). strip () 42 if city in menu [province]: 43 while q_flag and Q_flag: 44 for I in menu [province] [city]: 45 print (I) 46 county = Input ('Enter the region or county (q, return or Q, exit ):'). strip () 47 if county in menu [province] [city]: 48 while q_flag and Q_flag: 49 for I in menu [province] [city] [county]: 50 print (I) 51 # The last page is displayed. The input format is 52 choice = input ('Last page! Q, return or Q, exit :'). strip () 53 if choice = 'q': 54 # exit the layer loop with else: q_flag = True, and return the upper layer loop 55 q_flag = False56 elif choice = 'q ': 57 # exit the Large Loop 58 Q_flag = False59 else: 60 # prompt that the input is invalid 61 print ('input error! ') 62 # change the value of q_flag to exit a loop of 63 else: 64 q_flag = True65 elif county = 'q ': 66 q_flag = False67 elif county = 'q': 68 Q_flag = False69 else: 70 print ('input error! ') 71 else: 72 q_flag = True73 elif city = 'q': 74 q_flag = False75 elif city = 'q': 76 Q_flag = False77 else: 78 print ('input error! ') 79 else: 80 q_flag = True81 elif province = 'q': 82 Q_flag = False83 else: 84 print ('input error! ')

 

Version 2

1 # _ author: Ahern Li 2 # @ _ date: 3 4 menu = {5 'zhejiang province ': {6 'hangzhou City': {7 'yuhang district ': {'zhongta' :{}, 'linping' :{}}, 8 'xihu district ': {'xihu' :{}, 'left' :{}} 9 }, 10 'wenzhou City ': {11 'cangnan County': {'lingxi' :{}, 'longgang ':{}, 12 'ruian County': {'anyang ':{}, 'jinhu ': {}}13} 14}, 15 'guangdong province': {16 'guangzhou City ': {17 'yuexiu district': {'renmin road ':{}, 'beijing': {}, 18 'liwan district ': {'sandup' :{}, 'longjin' :{}} 19}, 20 'zhuhai City ': {21 'xiangzhou district ': {'gongbei': {}, 'geida ':{}}, 22 'jinwan district': {'hongqi town ':{}, 'pingsha town ': {}} 23} 24} 25} 26 27 current_layer = menu # Implement Dynamic Loop 2 8 parent_layer = [] # retain all parent layers. The last element is always the parent layer 29 30 while True: 31 print () # Only for beautiful print 32 for I in current_layer: # print URL 33 print (I) 34 choice = input ('enter the query URL \ n >> :'). strip () 35 if choice in current_layer: 36 if current_layer [choice]: # determine whether it is the last layer 37 parent_layer.append (current_layer) # before entering the child layer, add the current layer as parent layer 38 current_layer = current_layer [choice] # modify child layer 39 else: 40 print ('current is the last page') 41 elif choice = '': 42 continue43 Elif choice = 'q': # Return the upper layer 44 if parent_layer: # determine whether parent_layer is null 45 current_layer = parent_layer.pop () # retrieve the current layer parent layer 46 # exit loop 47 elif choice = 'q': 48 break49 else: 50 print ('input error! ')

 

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.