使用python实现省市三级菜单效果
地区分三层结构例如:
大中华地区一级划分:
华东
华中
华北
西南
特别行政区
华南
-------------------------------------------------
请输入你要查看的大中华地区名字:华中
------------------包含的省名字二级:-----------------
湖北
湖南
河南
-------------------------------------------------
请输入你要查看的省名字:湖北
--------------包含的城市名三级:-------------------
程序大概的思路和逻辑:
1 将大中华地区划用字典列表来构造,用key取省或者地市列表,用for遍历显示字典或者列表的元素
2 根据整个字典的key,得到省列表,遍历打印省的名字列表
3 设置用于跳出外循环的flag(跳出嵌套循环)
4 两层嵌套循环,外循环for指定3次固定循环,3次输入省名字错误,程序退出(在外循环for后,通过else来实现)
内循环while True是死循环,不指定次数(地市名字输入错误后,会一直要求输入,直到输入正确为止),通过break退出内循环
5 通过整个字典的key得到省列表,通过省字典的key得到地市列表,遍历打印地市的名字列表
6 判断输入的地市是否在省字典中,在的话,通过省字典的key,for遍历打印城市的名字列表不在的话,提示"输入的省名字不对,请重新输入",continue跳出当次迭代(循环),继续输入省名字还在内循环while中
7 打印城市表后,2个选择,1是退出整个程序(2个嵌套循环都退出),2是回到上一级别菜单(只需要退出内循环的当前迭代)
8 回到上一级别菜单(只退出内循环while) 判断用户输入的是否是"b",是的话,continnue 退出内循环的当前迭代,开始内循环下次迭代
9 退出整个程序(2个嵌套循环都退出) 判断用户输入的是否是"q",是的话,先将flag变成True,然后break退出while内循环
10 再做一个判断,判断flag是否是True,如果是的话,break退出for外循环,至此,退出整个程序(2个嵌套循环都退出)
流程图:
代码:
#!/user/binenv python3 # -*- coding:utf-8 -*- ''' Created on: 2015年1月16日 @author: 吕毅 Email: 371725153@qq.com Version: 1.0 ''' china_map ={ "华南":{ "广东":["广州市","佛山市","深圳市","东莞市"], "广西":["南宁市","柳州市","桂林市","北海市"], "海南":["海口市","三亚市","三沙市","儋州市"] }, "华东":{ "上海":["黄浦区","卢湾区","徐汇区","长宁区","普陀区"], "安徽":["合肥市","芜湖市","淮南市","马鞍山市"], "江苏":["南京市","无锡市","徐州市","常州市","苏州市"] }, "华北":{ "北京":["东城区","西城区","朝阳区","丰台区","石景山区","海淀区"], "山西":["太原市","大同市","阳泉市","长治市"], "河北":["石家庄市","唐山市","秦皇岛市","邢台市"] }, "华中":{ "湖北":["武汉市","黄石市","十堰市","十堰市"], "河南":["郑州市","开封市","洛阳市","平顶山市"], "湖南":["长沙市","株洲市","衡阳市","邵阳市"] }, "西南":{ "重庆":["万州区","涪陵区","渝中区","大渡口区"], "四川":["成都市","自贡市","攀枝花市","德阳市"], "贵州":["贵阳市","六盘水市","遵义市","安顺市"], }, "特别行政区":{ "香港":["屯门","弯仔","北角","西贡"], "澳门":["花地玛堂区","圣安多尼堂区","大堂区","望德堂区"], }, } print ("-------------------------------------------------") print ("+ +") print ("+ +") print ("+ 欢迎来到大中华地区查询系统地 +") print ("+ +") print ("+ +") print ("-------------------------------------------------") print ("大中华地区一级划分:") for i in china_map: #遍历字典的key,列出大中华地区的名字 print (i) print ("-------------------------------------------------") jump_flag = False #用于跳出外循环 for i in range(3): #外循环,指定循环3次,3次外循环完了,就退出程序 greater_china_name = input("请输入你要查看的大中华地区名字:") if greater_china_name in china_map: #检查输入的地区是否在地图中,如果地区名字3次输入错误,程序退出 gc_name = china_map[greater_china_name] province_name = gc_name.keys()#使用输入的信息作为key,取出省信息,存在字典中 while True: #内循环,死循环,不指定循环次数,通过break或者flag跳出 print ("------------------包含的省名字二级:-----------------") #分隔线 for i in province_name: #遍历列表,取出省名字,打印出来 print (i) print ("-------------------------------------------------") #分隔线 sheng_name_input = input("请输入你要查看的省名字:") if sheng_name_input in province_name: #判断输入的省名字是否在地区列表中 shi_name = china_map[greater_china_name][sheng_name_input] #取出省中有哪些市,存在列表中 print ("--------------包含的城市名三级:-------------------") #分隔线 for i in shi_name: #遍历列表,取出地区市名字,打印出来 print (i) print ("------------------------------------------------") #分隔线 if sheng_name_input not in province_name: #如果输入的省名字不在在地区列表中 print ("输入的省名字不对,请重新输入") continue #跳出当次迭代,开始下一次迭代循环,直到地市名字输入正确为止(不停的要求输入) back_or_quit = input("请问是否退出?按b:Back是返回上一级菜单;按q:Exit是退出整个程序") #显示完地区市后,就要退出程序了,一个是全部退出,一个是返回上一级菜单 if back_or_quit == "q": jump_flag = True #用于跳出外循环 break #跳出while内循环 if back_or_quit == "b": continue # 跳出当次迭代,开始下一次迭代循环,重新输入省处,返回上一步 print ("你输入的信息有误,请重新输入") if jump_flag: #跳出外循环的条件满足 break #跳出外循环 else:#上面的3次for循环正常执行完毕,else才会执行,如果是不正常退出(break),else不会执行 print ("3次输入错误,程序退出")

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

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



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

MySQL download file is corrupt, what should I do? Alas, if you download MySQL, you can encounter file corruption. It’s really not easy these days! This article will talk about how to solve this problem so that everyone can avoid detours. After reading it, you can not only repair the damaged MySQL installation package, but also have a deeper understanding of the download and installation process to avoid getting stuck in the future. Let’s first talk about why downloading files is damaged. There are many reasons for this. Network problems are the culprit. Interruption in the download process and instability in the network may lead to file corruption. There is also the problem with the download source itself. The server file itself is broken, and of course it is also broken when you download it. In addition, excessive "passionate" scanning of some antivirus software may also cause file corruption. Diagnostic problem: Determine if the file is really corrupt

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.
