Multi-level menu operation (function implementation)
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:
香港: 香港 澳门: 澳门 台湾: 台湾 钓鱼岛: 钓鱼岛 北京市: - 市辖区 - 东城区 - 西城区 - 崇文区 - 宣武区 - 朝阳区 河北省: - 石家庄市:- 长安区- 桥东区- 桥西区- 新华区 - 唐山市:- 路南区- 路北区- 古冶区- 开平区 - 秦皇岛市:- 海港区- 山海关区 山西省: - 太原市:- 小店区- 迎泽区- 杏花岭区 - 大同市:- 城 区- 矿 区- 南郊区 - 阳泉市:- 城 区- 矿 区- 郊 区- 平定县- 盂 县 - 长治市:- 城 区- 郊 区- 长治县- 襄垣县- 屯留县
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:
,) =
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: 代码如下: 上面代码,我学到了如下知识: (1)yaml格式文件的处理,导入yaml模块,使用yaml.load()进行文件字符串转换为字典形式; (2)全局变量的定义,在很多时候,在程序运行的过程中,我们总是需要的到几个重要的变量,使用全局变量,能够让我们更好的理清思路,全局变量使用global来进行定义; (3)函数def的结束是遇到return,遇到return无论什么时候,就结束这个函数,因为当我们与while结合使用的时候,可以直接使用return来结束这个循环; (4)字典、列表的处理方式; (5)enumerate()函数的使用; (6)sys.exit()来退出整个程序,sys模块; (7)不怕麻烦,牢牢知道自己需要的是什么自己的目的是什么。 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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

Apple brought some Pro-exclusive hardware features to iPhone 15 Pro and 15 Pro Max, which attracted everyone’s attention. We're talking titanium frames, sleek designs, the new A17 Pro chipset, an exciting 5x telephoto lens, and more. Of all the bells and whistles added to the iPhone 15 Pro models, the action button remains a prominent and prominent feature. Needless to say, it is a useful addition to launching actions on your iPhone. That said, you could accidentally hold down the Action button and trigger the feature inadvertently. Frankly, it's annoying. To avoid this, you should disable the action button on iPhone 15 Pro and 15 Pro Max. let

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

CSS web page scroll monitoring: monitor web page scroll events and perform corresponding operations. With the continuous development of front-end technology, the effects and interactions of web pages are becoming more and more rich and diverse. Among them, scroll monitoring is a common technology that can perform some special effects or operations based on the scroll position when the user scrolls the web page. Generally speaking, scroll monitoring can be implemented through JavaScript. However, in some cases, we can also achieve the effect of scroll monitoring through pure CSS. This article will introduce how to implement scrolling of web pages through CSS

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq
