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!

PHP编程中有哪些常见的Behat操作?Behat是一个行为驱动开发(BDD)工具,允许测试人员和开发人员在自然语言中撰写测试用例,并将这些用例转化为可执行的代码。它支持PHP语言,并提供了丰富的库和功能,可以实现多种常见的测试操作。下面列举了PHP编程中常见的Behat操作。前置条件(Background)在编写测试用例时,有时候会有一些公共的前置条件需要

ThinkPHP6是一款基于PHP的MVC框架,极大地简化了Web应用程序的开发。其中表单验证是一个非常基础和重要的功能。在这篇文章中,我们将介绍ThinkPHP6中如何进行表单验证操作。一、验证规则定义在ThinkPHP6中,验证规则都需要定义在控制器中,我们可以通过在控制器中定义一个$validate属性来实现规则的定义,如下所示:usethinkVa

PHP编程中有哪些常见的jQuery操作?在PHP编程中,使用jQuery进行网页开发是一种非常方便和高效的方式。jQuery是一个简单而强大的JavaScript库,包含了许多实用的方法和函数。在PHP编程中,我们常常使用jQuery来操纵HTML和DOM元素,使网页具有更好的交互性和高度的可视化效果。在本文中,我们将介绍一些常见的PHP编程中使用jQue

OAuth(开放授权)是一种用于授权访问控制的标准化协议。在Web开发中,使用OAuth可以帮助应用程序安全地从第三方平台中获取用户数据或资源。而在PHP编程中,OAuth操作也被广泛应用。本文将介绍PHP编程中的常见OAuth操作。OAuth1.0a授权OAuth1.0a授权是OAuth中最早出现的授权方式,也是最复杂的一种授权方式。在PHP编程中,O

随着互联网应用的不断发展,搜索引擎也成为了日常生活中必不可少的工具,而分词搜索是搜索引擎中非常重要的一种搜索方式。在使用ThinkPHP6框架开发项目时,我们也需要对分词搜索进行深入了解和应用。本文将介绍ThinkPHP6中如何进行分词搜索操作。一、分词搜索简介分词搜索是将用户输入的关键词进行分割,然后在数据库中进行模糊搜索,找到相符合的记录。相较于传统的搜

随着全球化的发展,越来越多的网站和应用程序需要提供多语言支持。而对于使用ThinkPHP6框架的开发者来说,如何实现多语言翻译操作是一个重要的需求。本文将介绍怎样使用ThinkPHP6进行多语言翻译操作。配置语言包在ThinkPHP6中,语言包是一个包含键值对的数组。可以将其存储在app/lang/目录下的各种子目录中。例如:/app/lang/zh-cn/

随着互联网的快速发展,基于图形的验证码已经成为了网站安全保障的一个重要环节。验证码可以有效地防止机器人或恶意程序对网站进行自动化操作,同时也可以保障用户信息的安全性。而在基于ThinkPHP6的网站开发中,如何实现captcha图形验证码的操作呢?本文将为您介绍具体的操作流程。一、生成Captcha图形验证码1、使用captcha库进行安装在ThinkPHP

近年来,邮件作为一种最为常见的通信方式,被广泛应用于各种应用场景中。在不同的WEB应用中,也经常需要通过发送邮件的方式来进行通知、验证等功能。而在使用ThinkPHP6框架开发WEB应用的过程中,我们需要了解如何进行邮件发送操作,以便更好地实现各种功能。下面我们将介绍如何在ThinkPHP6中进行邮件发送操作。配置邮件在ThinkPHP6中配置邮件非常方便。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
