Using Python to achieve three-level menu effects in cities and provinces

Source: Internet
Author: User
The area is divided into three-storey structures such as:

Greater China-level division:

East

Central

North

Southwest

Sar

South

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

Please enter the Greater China region name you want to view: Huazhong

------------------contains the province name level two:-----------------

Hubei

Hunan

Henan

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

Please enter the province name you want to view: Hubei

--------------contains city name level three:-------------------

The approximate idea and logic of the program:

1 Use a dictionary list to construct the Greater China region, take a list of provinces or cities with key, and use for traversal to display the elements of a dictionary or list

2 according to the whole dictionary key, get the province list, traverse the Print province Name list

3 set flag to jump out of outer loop (out of nested loops)

42-Layer nested loop, outer loop for specify 3 fixed loops, 3 input province name error, program exit (outside loop for, through else)

Inner Loop While true is a dead loop and does not specify the number of times (after the city name is entered incorrectly, the input is always required until the input is correct), and the inner loop is exited by break

5 through the whole dictionary key to get the province list, through the province dictionary key to get a list of the city, traverse the name list of the print city

6 Determine whether the input of the city is in the province dictionary, in the words, through the province dictionary key,for traversal print city Name list is not, prompt "input province name is wrong, please re-enter", continue jump out when iteration (loop), continue to enter the province name is still inside the loop while

7 After printing the city table, 2 options, 1 is to exit the entire program (2 nested loops are exited), 2 is back to the previous level menu (only need to exit the current iteration of the inner loop)

8 go back to the previous level menu (just exit the inner loop while) to determine if the user entered is "B", if so, continnue exits the current iteration of the inner loop, and the next iteration within the loop begins

9 Exit the entire program (2 nested loops are exited) to determine whether the user input is "q", if so, the flag will first become true, and then break exit while inside the loop

10 again make a judgment, determine whether flag is true, if so, break exits for Outer loop, so far, exit the entire program (2 nested loops are exited)

Flow chart:

Code:

#!/user/binenv python3#-*-coding:utf-8-*-"Created on:2015 year January 16 @author: Yi Lv email:371725153@qq.com version:1.0 ' ' China_map ={' South China: {"Guangdong": ["Guangzhou", "Foshan", "Shenzhen", "Dongguan"], "Guangxi": ["Nanning", "Liuzhou", "Guilin", "Beihai"], "Hainan": ["Haikou", "Sanya", "Three Shashi", "Zhanzhou" ]}, "East China": {"Shanghai": ["Huangpu", "Luwan", "Xuhui", "changning", "Putuo"], "Anhui": ["Hefei", "Wuhu", "Huainan", "Maanshan"], "Jiangsu": ["Nanjing", "Wuxi", "Xuzhou", "Changzhou", "su State City "]}," North China: {"Beijing": ["Dongcheng", "Xicheng", "Chaoyang", "Fengtai", "Shijingshan", "Haidian"], "Shanxi": ["Taiyuan City", "Datong", "Yangquan", "Changzhi"], "Hebei": ["Shijiazhuang City", "Tangshan", "Qinhuangdao   City "," Xingtai "]}," Central ": {" Hubei ": [" Wuhan "," Huangshi "," Shiyan "," Shiyan "]," Henan ": [" Zhengzhou "," Kaifeng "," Luoyang "," Pingdingshan "]," Hunan ": [" Changsha "," Zhuzhou "," Hengyang "," Shaoyang "] }, "Southwest": {"Chongqing": ["Wanxian District", "Fuling District", "Yuzhong District", "Dadukou District"], "Sichuan": ["Chengdu City", "Zigong", "Panzhihua", "Deyang"], "Guizhou": ["Guiyang", "Liupanshui", "Zunyi", "Anshun"],}, "Special Administrative Region ": {" Hong Kong ": [" Tunmen "," Bend Aberdeen "," North Point "," Sai Kung "]," Macau ": [" Martine "," St. Anthony's Parish "," lobby area "," Wangdet "],},} print ("------------------------------            -------------------") Print (" + + ") print (" + + ") print (" + Welcome to Greater China Area Query system + ") print (" + + ") Print (" + + ") pRint ("-------------------------------------------------") print ("Greater China Division:") for I in China_map: #遍历字典的key, List the Greater China Region name print (i) print ("-------------------------------------------------") Jump_flag = False #用于跳出外循环for i in range (3): #外循环, specify the Cycle 3 times, 3 times out of circulation, the exit program Greater_china_name = input ("Please enter the Greater China region name you want to view:") if Greater_china_name in China_map: # Check whether the input region is in the map, if the region name 3 times input error, the program exits gc_name = china_map[greater_china_name] Province_name = Gc_name.keys () #使用输入的信息作为key,   Remove the province information, exist in the dictionary while True: #内循环, dead Loop, do not specify the number of loops, jump out of print by break or flag ("------------------contains the province name level two:-----------------") #分隔线   For i in Province_name: #遍历列表, take out the province name, print it (i) print ("-------------------------------------------------") #分隔线 Sheng_name_input = input ("Please enter the province name you want to view:") if sheng_name_input in province_name: #判断输入的省名字是否在地区列表中 shi_name = China_ Map[greater_china_name][sheng_name_input] #取出省中有哪些市, there is a print in the list ("--------------contains the city name level three:-------------------") # Divider for I in Shi_name: #遍历列表, take out the name of the local city, print it out (i)   Print ("------------------------------------------------") #分隔线 if Sheng_name_input not in Province_name: #如果输入的省名字不 Print in the region list ("Input province name is incorrect, re-enter") Continue #跳出当次迭代, start the next iteration loop until the name of the city is entered correctly (keep asking for input) Back_or_quit = input ("Would you like to exit?"). Press B:back to return to the previous menu; press Q:exit to exit the whole program ") #显示完地区市后, to quit the program, one is all quit, one is to return to the previous menu if back_or_quit = =" Q ": Jump_flag = True #用于跳出  Outer loop Break #跳出while内循环 if back_or_quit = = "B": Continue # Jumps out of the iteration, starts the next iteration loop, re-enters the province, and returns to the previous print ("You entered the wrong information, please re-enter") If Jump_flag: #跳出外循环的条件满足 break #跳出外循环else: #上面的3次for循环正常执行完毕, Else will execute if exit is not normal (break), else will not execute print ("3 input error, 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.