Day1 job 2: multi-level menu operations, day1 job

Source: Internet
Author: User
Tags add numbers

Day1 job 2: multi-level menu operations, day1 job

Job 2: multi-level menu

(1) Level 3 menu

(2) You can choose to enter each sub-menu.

(3) New knowledge points: List and Dictionary

Requirement: Enter back to return to the previous layer, and enter quit to exit the entire program.

Ideas:

(1) define a three-level menu dictionary;

(2) extract the number of the first-level province, print the provinces included, and let the user enter the province number to display the corresponding province. In this process, you need to create a dictionary, stores the original name and number of the province, so that the user can enter and match;

(3) enter the second-level menu, city extraction, and city storage in a dictionary, so that users can search in the dictionary when entering the corresponding number, and return to the corresponding city;

(4) based on the input above, obtain the list of counties/districts, traverse the list, and use enumerate () to add numbers to counties/districts. The corresponding indexes only need to be reduced by one, because it is impossible for users to see that the list starts from 0 by default;

The flowchart is as follows:

The Code is as follows:

1 import collections 2 dict_db = {"Henan": [3 {"Zhengzhou": ["Central Plains", "jinshui district", "second seven district", "shangjie district", "Gongyi City ", "Xinzheng City", "Xinmi City", "DengFeng city", "piyang City", "zhongmu county"]}, 4 {"Kaifeng": ["Gulou District", "longting district ", "yuwangtai district", "Jinming district", "Kaifeng County", "zhushi county", "Lankao County", "zhuxian", "tongxu county"]}, 5 {"Nanyang ": ["Wolong district", "Wancheng District", "dengzhou City", "Tongbai County", "Fangcheng County", "Tongchuan county", "Zhenping county", "Tanghe County", "Nanzhao County ", "Neixiang County"]}, 6 {"Luoyang": ["xigong district", "Old City", "luolong district", "Geely district ", "yanshi City", "Mengjin county", "Ruyang County"]}, 7 8], 9 "Hunan": [10 {"Changsha": ["Yuelu district", "Furong district ", "Tianxin district", "Kaifu district", "Yuhua district", "Liuyang City", "Changsha county"]}, 11 {"Yueyang": ["Yueyang Loud.com "," Yunxi district "," junshan district "," linxiang City "," Miluo City "," Yueyang County "]}, 12 {" Zhuzhou ": [" tianyuan district ", "hetang district", "lusong district", "Shifeng district", "Liling City", "Zhuzhou county", "Yanling county"] },13 {"Xiangtan": ["Yuetang district ", "yuhu district", "Xiangxiang City", "Shaoshan City", "Xiangtan County"]}, 14], 15 "Guangdong": [16 {"Guangzhou": ["Yuexiu District ", "liwan district", "Haizhu District", "Tianhe District", "Baiyun district", "Huangpi District", "Panyu District", "Huadu District"]}, 17 {"Shenzhen ": ["Futian district", "Luohu District", "Nanshan District", "Baoan District", "Longgang District", "Yancheng district"] },18 {"Zhuhai": ["Xiangzhou District ", "doumen district", "jinwan district"]}, 19] 20} 21 22 if _ name _ = "_ main _": 23 active = True24 # program identifier, when does the control program stop? Because it contains multiple layers of loops, the entire loop is terminated when active is set to False. 25 # user input In quit, active = False ends the entire loop. When the user inputs back, break exits the current loop and jumps back to the previous loop 26 while active: 27 print ("welcome to the provincial/municipal inquiry system... ") 28 p_dict ={}# user ID and province, so that the user can enter the ID 29 print (" + ----------------------------------------------------------------- + ") 30 for p_num, p_key in enumerate (dict_db.keys (), 1): # enumerate () add ID 31 p_dict [p_num] = p_key32 33 print ("province ID: % d province Name: % s "% (p_num, p_key) 34 print (" + ---------------------------------- ----------------------------- + ") 35 find_p = input (" Enter the province ID you want to query (enter quit to exit): ") 36 if find_p =" quit ": 37 break38 39 elif find_p.isdigit () and int (find_p) <= len (p_dict): 40 print ("\ 033 [32; 1 m % s \ 033 [0 m "% (p_dict [int (find_p)]) # color the output 41 province = p_dict [int (find_p)] 42 # enter the second-level loop 43 while active: 44 cities = dict_db [province] 45 cities_dict ={}# store the city corresponding to the province, the user can enter the number to display the corresponding 46 print ("+ ---------------------- city name -------------------- Copyright + ") 47 for c_num, city_dict in enumerate (cities, 1): 48 for city in city_dict.keys (): 49 print (" city No.: % s city name: % s "% (c_num, city) 50 cities_dict [c_num] = city51 print (" + ----------------------------------------------------------------- + ") 52 find_city_num = input ("enter the city number you want to query (enter quit to exit, and enter back to return to the previous layer):") 53 if find_city_num = "quit ": 54 active = False55 elif find_cit Y_num = "back": 56 break57 elif find_city_num.isdigit () and int (find_city_num) <= len (cities): 58 print ("\ 033 [22; 1 m % s \ 033 [0 m "% (p_dict [int (find_p)], cities_dict [int (find_city_num)]) 59 while active: 60 # enter the third-tier cycle 61 countries = cities [int (find_city_num)-1] [cities_dict [int (find_city_num)] 62 print ("+ counter +") 63 for country_num, country in enumer Ate (countries, 1): 64 print ("county/district No.: % s county/district name: % s" % (country_num, country )) 65 print ("+ outputs +") 66 find_country_num = input ("Enter the ID of the county/district you want to query (enter quit to exit, enter back to return to the previous layer ):") 67 if find_country_num = "quit": 68 active = False69 elif find_country_num = "back": 70 break71 elif partition () and int (find_country_num) <= len (countries ): 72 print ("\ 033 [1; 1 m % s \ 033 [0 m "% (p_dict [int (find_p)], cities_dict [int (find_city_num)], countries [int (find_country_num)-1]) 73 else: 74 print ("sorry, the number you entered is incorrect. Please check and re-enter") 75 else: 76 print ("sorry, the number you entered is incorrect. check the number and try again! ") 77 active = False78 79 80 else: 81 print (" the number you entered is incorrect. check the number and try again! ")

The code execution result is as follows:

Welcome to the provincial/municipal query system...
+ --------------------------------------------------------------- +
Province No.: 1 province name: Guangdong
Province No.: 2 province name: Henan
Province No.: name of Province 3: Hunan
+ --------------------------------------------------------------- +
Enter the province ID you want to query (enter quit to exit): 2
Henan
+ --------------------------------------------------------------- +
City No.: 1 city name: Zhengzhou
City No.: 2 city name: Kaifeng
City No.: 3 city name: Nanyang
City No.: 4 city name: Luoyang
+ --------------------------------------------------------------- +
Enter the city number you want to query (enter quit to exit, enter back to return to the previous layer): 3
Nanyang, Henan Province
+ --------------------------------------------------------------- +
County/district No.: 1 county/district name: Wolong District
County/district No.: 2 county/district name: Wancheng District
County/district No.: 3 county/district name: dengzhou City
County/district No.: 4 county/district name: Tongbai County
County/district No.: 5 county/district name: Fangcheng County
County/district No.: 6 county/district name: huangchuan County
County/district No.: 7 county/district name: Zhenping County
County/district No.: 8 county/district name: Tanghe County
County/district No.: 9 county/district name: Nanzhao County
County/district No.: 10 county/district name: Neixiang County
+ --------------------------------------------------------------- +
Enter the ID of the county/district you want to query (enter quit to exit, enter back to return to the previous layer): 4
Tongbai County, Nanyang City, Henan Province
+ --------------------------------------------------------------- +
County/district No.: 1 county/district name: Wolong District
County/district No.: 2 county/district name: Wancheng District
County/district No.: 3 county/district name: dengzhou City
County/district No.: 4 county/district name: Tongbai County
County/district No.: 5 county/district name: Fangcheng County
County/district No.: 6 county/district name: huangchuan County
County/district No.: 7 county/district name: Zhenping County
County/district No.: 8 county/district name: Tanghe County
County/district No.: 9 county/district name: Nanzhao County
County/district No.: 10 county/district name: Neixiang County
+ --------------------------------------------------------------- +
Enter the ID of the county/district you want to query (enter quit to exit, enter back to return to the previous layer): back
+ --------------------------------------------------------------- +
City No.: 1 city name: Zhengzhou
City No.: 2 city name: Kaifeng
City No.: 3 city name: Nanyang
City No.: 4 city name: Luoyang
+ --------------------------------------------------------------- +
Enter the city number you want to query (enter quit to exit, enter back to return to the previous layer): 2
Kaifeng, Henan Province
+ --------------------------------------------------------------- +
County/district No.: 1 county/district name: Gulou District
County/district No.: 2 county/district name: longting District
County/district No.: 3 county/district name: wangtai District
County/district No.: 4 county/district name: Jinming District
County/district No.: 5 county/district name: Kaifeng County
County/district No.: 6 county/district name: zhushi County
County/district No.: 7 county/district name: Lankao County
County/district No.: 8 county/district name: PiXian
County/district No.: 9 county/district name: tongxu County
+ --------------------------------------------------------------- +
Enter the ID of the county/district you want to query (enter quit to exit, enter back to return to the previous layer): back
+ --------------------------------------------------------------- +
City No.: 1 city name: Zhengzhou
City No.: 2 city name: Kaifeng
City No.: 3 city name: Nanyang
City No.: 4 city name: Luoyang
+ --------------------------------------------------------------- +
Enter the city number you want to query (enter quit to exit, enter back to return to the previous layer): back
Welcome to the provincial/municipal query system...
+ --------------------------------------------------------------- +
Province No.: 1 province name: Guangdong
Province No.: 2 province name: Henan
Province No.: name of Province 3: Hunan
+ --------------------------------------------------------------- +
Enter the province ID you want to query (enter quit to exit): quit

Resolution:

In the multi-level menu, you must enter quit to exit the entire loop, and enter back to return to the previous layer. This reminds us that we need to set the operation identifier of all loops in a program, regardless of the stage, as long as you enter quit, you can exit the entire loop. In the above program, we set the program running identifier active, which defaults to True. When we end the program, set "China" and "identifier" to "False" to terminate the entire program. When you enter "back", you only need to use break to end the current loop, the program will exit the previous loop, and our goal will be achieved.

The above loop uses a hierarchical loop, that is, the following:

While True:

Pass

While True:

Pass

While True:

Pass

In traversal, we need to know the attributes of the object, including the dictionary, list, and string. Only in this way can we use the corresponding methods for corresponding operations.

The enumerate () function is used to add sequence numbers to elements when traversing the List objects. You can specify the actual position. For example, the product number cannot start from 0, which does not meet the public's habits.

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.