Use python to implement provincial/municipal menus

Source: Internet
Author: User
This article will share with you the code that uses python to implement provincial/municipal levels of menu effects. It is very practical. If you need it, you can refer to it. The region is divided into three layers, for example:

Level 1 division in Greater China:

East China

Central China

China North

Southwest China

Special Administrative Region

South China

-------------------------------------------------

Enter the name of the Greater China region you want to view: Central China

------------------ Contains the province name Level 2 :-----------------

Hubei Province

Hunan

Henan

-------------------------------------------------

Enter the name of the province you want to view: Hubei

-------------- City name Level 3 :-------------------

General Program ideas and logic:

1. Use the dictionary list to construct the large and medium-sized Chinese regions. Use the key to retrieve the list of provinces or cities, and use for to traverse the elements that display the dictionary or list.

2. Obtain the province list based on the key of the entire dictionary and print the name list of the province.

3. Set the flag used to jump out of the External Loop (jump out of the nested loop)

4. Two-layer nested loop. The External Loop for specifies three fixed loops, three input province names are incorrect, and the program exits (after the External Loop for, it is implemented through else)

Inner Loop while True is an endless loop and does not specify the number of times (after the city name is entered incorrectly, it will always require the input until the input is correct), exit the inner loop through break

5. Obtain the province list using the entire dictionary key, obtain the city list using the province dictionary key, and print the name list of the city through traversal.

6. Check whether the entered city is in the province dictionary. If the province dictionary key is used, if the name list of the printed city is not displayed, the system prompts "the province name entered is incorrect, please enter ", continue to jump out of the current iteration (loop), continue to enter the province name in the loop while

7. After the city table is printed, two options are selected. One is to exit the entire program (two nested loops exit), and the other is to return to the previous menu (only the current iteration of the inner loop needs to exit)

8. Return to the previous menu (only exit the inner loop while) and check whether the user inputs "B". If yes, continnue exits the current iteration of the inner loop and starts the next iteration of the inner loop.

9 exit the entire program (two nested loops exit) to determine whether the user input is "q". If yes, the flag is changed to True first, and then the break exits the while loop.

10. Make another judgment to determine whether the flag is True. If yes, break exits the for outer loop. At this point, the entire program is exited (both nested loops exit)

Flowchart:

Code:

#! /User/binenv python3 #-*-coding: UTF-8-*-'''created on: January 16, 2015 @ author: Lu Yi Email: 371725153@qq.com Version: 1.0 ''' china_map = {"South China": {"Guangdong": ["Guangzhou City", "Foshan City", "Shenzhen City", "Dongguan City"], "Guangxi ": ["Nanning City", "Liuzhou City", "Guilin City", "Beihai City"], "Hainan": ["Haikou City", "Sanya City", "sansha City ", "Bozhou city"]}, "East China": {"Shanghai": ["Huangpu District", "Luwan District", "Xuhui District", "Changning district", "Putuo District"], "Anhui": ["Hefei city", "Wuhu City", "HuaiNan City", "Maanshan City"], "Jiangsu": ["Nanjing City", "Wuxi City", "Xuzhou City ", "Changzhou City", "Suzhou City"]}, "North China": {"Beijing": ["Dongcheng District", "Xicheng district", "Chaoyang District", "Fengtai District", "Shijingshan district", "Haidian District"], "Shanxi": ["Taiyuan City", "Datong City", "Yangquan City", "Changzhi City"], "Hebei": ["Shijiazhuang City ", "Tangshan City", "Qinhuangdao City", "Xingtai City"]}, "Central China": {"Hubei": ["Wuhan City", "Huangshi city", "Shiyan city ", "Shiyan City"], "Henan": ["Zhengzhou City", "Kaifeng City", "Luoyang City", "Pingdingshan City"], "Hunan": ["Changsha City", "Zhuzhou city ", "Hengyang City", "Shaoyang City"]}, "Southwest China": {"Chongqing": ["Wanzhou District", "Fuling District", "Yuzhong District", "dadukou district"], "Sichuan": ["Chengdu", "Zigong City", "Panzhihua City", "Deyang City"], "Guizhou": ["Guiyang City", "liupanshui City", "Zunyi City ", "Anshun City"] ,}, "Special Administrative Region": {"Hong Kong": ["Tuen Mun", "Qu Zi", "bei Jiao", "Sai Kung"], "Macao ": ["huadi Matang district", "San Andoni Hall district", "lobby area", "wangde Hall district"], },} print ("------------------- ---------------------------- ") Print (" ++ ") print (" ++ ") print (" + welcome to the Greater China region query system + ") print (" ++ ") print ("++") print ("-------------------------------------------------") print ("Greater China Level 1 division:") for I in china_map: # traverse the dictionary key, list print (I) print ("-----------------------------------------------------") jump_flag = False # used to jump out of an External Loop for I in range (3): # External Loop, specify 3 cycles, exit the program greater_china_name = input ("please Enter the name of the Greater China region you want to view: ") if greater_china_name in china_map: # Check whether the entered region is in the map. if the entered region name is incorrect three times, the program exits gc_name = china_map [greater_china_name] province_name = gc_name.keys () # uses the input information as the key to retrieve the province information, which is in the dictionary while True: # Inner Loop, endless loop, print ("-------------------- containing province name Level 2: ---------------") # separation line for I in province_name: # retrieve province name through the traversal list without specifying the number of cycles, print out print (I) print ("-------------------------------------------------")# Divider sheng_name_input = input ("Enter the province name you want to view:") if sheng_name_input in province_name: # determine whether the input province name is in the region list. In the region list, shi_name = china_map [greater_china_name] [sheng_name_input] # retrieve the cities in the province, and the cities in the print ("-------------- include the city name Level 3: ------------------- ") # separator line for I in shi_name: # traverse the list, retrieve the region name, print out print (I) print (" partition ") # separator line if sheng_name_input not in province_name: # If the province name is not in Print ("the province name entered is incorrect, Please re-enter") continue # Jump out of the current iteration and start the next iteration cycle, until the city name is entered correctly (enter continuously) back_or_quit = input ("do you want to exit? Press B: Back to return to the upper menu; press q: Exit to Exit the entire program ") # Exit the program after the region and city are displayed, one is to return the upper-level menu if back_or_quit = "q": jump_flag = True # used to jump out of the outer loop break # Jump Out Of The while loop if back_or_quit = "B ": continue # Jump out of the current iteration, start the next iteration cycle, re-enter the province, and return the previous print ("your input information is incorrect, Please re-enter") if jump_flag: # The condition for jumping out of the outer loop satisfies break # jumping out of the outer loop else: # the preceding three for loops are executed normally, and else will execute them. If it is an abnormal exit (break ), else does not execute print ("3 input errors, program exit ")

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.