Home > Backend Development > Python Tutorial > Multi-level menu operation (function implementation)

Multi-level menu operation (function implementation)

巴扎黑
Release: 2017-06-23 15:08:49
Original
2588 people have browsed it

Assignment 2: Multi-level menu

(1) Three-level menu

(2) You can choose to enter each submenu

(3) New knowledge points required: list, dictionary

Requirements: Enter back to return to the previous level, enter quit to exit the entire program

The third-level menu in this example is a yaml file format, the format is as follows:

香港:
  香港
澳门:
  澳门
台湾:
  台湾
钓鱼岛:
  钓鱼岛
北京市:  - 市辖区  - 东城区  - 西城区  - 崇文区  - 宣武区  - 朝阳区
河北省:  - 石家庄市:- 长安区- 桥东区- 桥西区- 新华区  - 唐山市:- 路南区- 路北区- 古冶区- 开平区  - 秦皇岛市:- 海港区- 山海关区
山西省:  - 太原市:- 小店区- 迎泽区- 杏花岭区  - 大同市:- 城 区- 矿 区- 南郊区  - 阳泉市:- 城 区- 矿 区- 郊 区- 平定县- 盂 县  - 长治市:- 城 区- 郊 区- 长治县- 襄垣县- 屯留县
Copy after login

The processing method of yaml format is as follows:

First of all, the above format is a multi-level The mode has level one, level two, and level three. The dictionary processed using yaml is in the following form:

,) =
Copy after login

The dictionary obtained by running is in the following format:

{'Hong Kong': 'Hong Kong',

'Beijing' : ['City-administered district', 'Dongcheng District', 'Xicheng District', 'Chongwen District', 'Xuanwu District', 'Chaoyang District'],

  'Taiwan': ' Taiwan',

##  'Macau': 'Macau',

'Hebei Province': [{'Shijiazhuang City': ['Chang'an District', 'Qiaodong District', 'Qiaoxi District', 'Xinhua District']}, {'Tangshan City': ['Lunan District', 'Lubei District', 'Guye District', 'Kaiping District' ]}, {'Qinhuangdao City': ['Haigang District', 'Shanhaiguan District']}],

  'Diaoyu Island': 'Diaoyu Island',

  'Shanxi Province': [{'Taiyuan City': ['Xiaodian District', 'Yingze District', 'Xinghualing District']}, {'Datong City': ['City District', 'Mining area', 'Southern suburbs']}, {'Yangquan City': ['City area', 'Mining area', 'Suburban area', 'Pingding County', 'Yu County']}, {'Changzhi City': [' Urban area', 'Suburban area', 'Changzhi County', 'Xiangyuan County', 'Tunliu County']}]}

What you get is a dictionary, the first level is like, "Hong Kong" : "Hong Kong", the value is a string type; the second level is: 'Beijing City': ['Municipal District', 'Dongcheng District', 'Xicheng District', 'Chongwen District', 'Xuanwu District', ' Chaoyang District'], the value is in the form of a list, with strings nested inside; the most complex is the three-level pattern, such as: 'Hebei Province': [{'Shijiazhuang City': ['Chang'an District ', 'Qiaodong District', 'Qiaoxi District', 'Xinhua District']}, {'Tangshan City': ['Lunan District', 'Lubei District', 'Guye District', 'Kaiping District'] }, {'Qinhuangdao City': ['Haigang District', 'Shanhaiguan District']}], the second level inside is a list, and the dictionary is nested in the list.

Ideas:

(1) The core of this project is actually to obtain provincial/ For city/county models, during the processing process, we judge based on the type corresponding to the value, which method is better to use;

(2 ) First, we traverse the first level of the dictionary, get the provinces, and put the provinces in a dictionary. In this way, we can output the corresponding provinces based on the values ​​entered by the user;

 (3) After getting the province, we can get the key value under the province, judge the type corresponding to the value, and perform different operations according to the type of the value;

(4) If the value is a string, it means that it is in the form of a first-level menu, which is the best way to handle it;

## (5) If the value is a list and the first value is a string, it means that it is a second-level mode like Beijing, and it is displayed using the second-level mode method;

 (6) If the value is a list, and the first value is in the form of a dictionary, it indicates that it is a three-level mode, and the three-level mode must be used to operate;

(7) To define global variables, we need to know what we need to get, we need to get the three-level model of provinces, cities and counties; only when we know the corresponding provinces, cities and counties, we only need to go to the dictionary You can get the value information by traversing it.

The flow chart is as follows:

   

代码如下:

 

                     s =                            province_dict =      num,province_name  enumerate(dic_db.keys(),                  province_dict[num] =         print( %(num,province_name) + ,end=                   num %  ==       print(     print(          find_index = input(     find_index =      find_index ==          sys.exit(      find_index          P_NAME =      elif find_index          P_NAME =               print(       P_NAME    #省的函数运行完成之后,返回省的名称,以便后续使用,因为主要是得到省/市/            city_name =      type(city_name) ==              elif type(city_name) ==          type(city_name[]) ==                       elif type(city_name[]) ==                               city_dict =      city_num,city_name  enumerate(city_list,         city_dict[city_num] =         print( %(city_num,city_name) + ,end=          city_num %  ==           print(         print(          city_index = input(         city_index =          city_index ==            city_index ==                       elif city_index              C_NAME =          elif city_index              C_NAME =                                    print(                      city_dict =      c_num,c_name  enumerate(country_list,          key              city_dict[c_num] =      k,v           print( %(k,v) + ,end=          k %  ==       print(     print(              city_num = input(         city_num =          city_num ==            city_num ==                       elif city_num              C_NAME =              key,value                   value ==                     C_INDEX =                            elif city_num              C_NAME =             C_INDEX =                       print(                                   countries = country_list[C_INDEX -               c_index,country  enumerate(countries,                 print( %(c_index,country) + ,end=                  k %  ==               print(             print(             country_index = input(             country_index =              country_index ==                country_index ==                               elif country_index               elif country_index  range(,len(countries)+                 show(province,C_NAME,countries[country_index-                              print(  def framework(province=,city=,country=          os.system(     message =  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                        \[;1m欢迎来到全国省市查询系统\  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  ***************************************************************************                  省  份:%                  市(区):%                  县(区):%  ***************************************************************************          print(message %  def show(province_name=,city_name=,country_name=     output =  ----------------------------------------------------------------------------            美丽的%s  %s  %      ----------------------------------------------------------------------------     \[0m     print(output %   __name__ ==      with open(,)          dic_db =                       ProvinceName =             judge(ProvinceName,dic_db)

代码运行如下:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                      欢迎来到全国省市查询系统

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

***************************************************************************

                省  份:

                市(区):

                县(区):

***************************************************************************
    
1. 澳门    2. 山西省    3. 北京市    4. 台湾    
5. 钓鱼岛    6. 香港    7. 河北省    
==============================================================
q:Exit
请输入你要查询的省的编号/名字:1

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

          美丽的澳门    

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1. 澳门    2. 山西省    3. 北京市    4. 台湾    
5. 钓鱼岛    6. 香港    7. 河北省    
==============================================================
q:Exit
请输入你要查询的省的编号/名字:2

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

          美丽的山西省    

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.太原市    2.大同市    3.阳泉市    4.长治市    

===============================================================================
q:Exit       b:退回上一层
请输入您要查询的市的编号/名字:2

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

          美丽的山西省  大同市  

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.城 区    
2.矿 区    
3.南郊区    

===============================================================================
q:Exit    b:退回上一层
请输入你要查询的县(区)编号/名字:2

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

          美丽的山西省  大同市  矿 区

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.城 区    
2.矿 区    
3.南郊区    

===============================================================================
q:Exit    b:退回上一层
请输入你要查询的县(区)编号/名字:b
请输入您要查询的市的编号/名字:b
1. 澳门    2. 山西省    3. 北京市    4. 台湾    
5. 钓鱼岛    6. 香港    7. 河北省    
==============================================================
q:Exit
请输入你要查询的省的编号/名字:北京市

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

          美丽的北京市    

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.市辖区    
==================================================================
q:Exit     b:退回上一层
请输入您要查询的市的编号/名字:1

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

          美丽的北京市  市辖区  

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1. 澳门    2. 山西省    3. 北京市    4. 台湾    
5. 钓鱼岛    6. 香港    7. 河北省    
==============================================================
q:Exit
请输入你要查询的省的编号/名字:7

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

          美丽的河北省    

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.石家庄市    2.唐山市    3.秦皇岛市    
===============================================================================
q:Exit       b:退回上一层
请输入您要查询的市的编号/名字:石家庄市

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

          美丽的河北省  石家庄市  

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.长安区    2.桥东区    3.桥西区    4.新华区    
===============================================================================
q:Exit    b:退回上一层
请输入你要查询的县(区)编号/名字:长安区

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

          美丽的河北省  石家庄市  长安区

               欢迎您!

               这里山美,水美,姑娘更美

----------------------------------------------------------------------------
    
1.长安区    2.桥东区    3.桥西区    4.新华区    
===============================================================================
q:Exit    b:退回上一层
请输入你要查询的县(区)编号/名字:q
Copy after login

 

    上面代码,我学到了如下知识:

  (1)yaml格式文件的处理,导入yaml模块,使用yaml.load()进行文件字符串转换为字典形式;

    (2)全局变量的定义,在很多时候,在程序运行的过程中,我们总是需要的到几个重要的变量,使用全局变量,能够让我们更好的理清思路,全局变量使用global来进行定义;

    (3)函数def的结束是遇到return,遇到return无论什么时候,就结束这个函数,因为当我们与while结合使用的时候,可以直接使用return来结束这个循环;

    (4)字典、列表的处理方式;

  (5)enumerate()函数的使用;

  (6)sys.exit()来退出整个程序,sys模块;

    (7)不怕麻烦,牢牢知道自己需要的是什么自己的目的是什么。

 

The above is the detailed content of Multi-level menu operation (function implementation). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template