python中用字典实现三级菜单
来源:互联网 发布:互联网软件开发工资 编辑:程序博客网 时间:2024/05/21 09:20
下面的代码为用字典数据类型实现一个三级菜单查找的功能,基本操作如下:
1. 菜单分为:省—市—县(区)三级。
2. 在任何一级的时候,用户都可以通过输入‘q’或者‘Q’结束(quit)程序。
3. 在省这一级的时候用户可以通过数据省的编号进入下一级菜单,输入无效字符会出现提示,并留在当前菜单。
4. 在市这一级的时候用户可以通过输入市的编号查看下属县(区),或者输入‘u’返回上一级(省)菜单。
输入无效字符会出现提示并留在当前菜单。
5. 在县这一级菜单的时候用户可以通过输入‘u’返回上一级(市),输入无效字符会出现提示。
# Author:Allen Liu# Data: 07/26/2017'''This program is a three-stage list which use dictionary realized'''data_dic = {'山东': {'菏泽': ['郓城', '曹县'], '济南': ['历城区', '长青区']},\ '江苏': {'苏州': ['沧浪','相城'], '南京': ['白下', '秦淮']}}# you can use this character to separate a line# use indicater to end the while loopindicater1 = Trueprovince_list = list(data_dic.keys())while indicater1: print(" Province ".center(50, '*')) for i in province_list: print(province_list.index(i)+1,': ',i) print('Attention: you can input \q or \Q to stop the program!') province_number = input('Please input the province number you want to request:') if province_number.isdigit():# judge the type of the input if int(province_number)>0 and int(province_number)<=len(province_list): indicater2 = True while indicater2: province_request = province_list[int(province_number)-1] print('The city of \'%s\' include:'.center(50, '*') % province_request) city_list = list(data_dic[province_request]) for j in city_list: print(city_list.index(j)+1,': ',j) city_number = input('You can input the number of the city to see the county or input \'u\' back to upper level: ') if city_number.isdigit(): if int(city_number)>0 and int(city_number)<=len(city_list): indicater3 = True while indicater3: city_request = city_list[int(city_number)-1] print('The county of \'%s\' include:'.center(50, '*') % city_request) county_list = list(data_dic[province_request][city_request]) for k in county_list: print(county_list.index(k) + 1, ': ', k) county_number = input('You can input \'u\' back to upper or just stop this level:') while indicater3: if county_number == 'u': indicater3 = False elif county_number.lower() == 'q': indicater1,indicater2,indicater3 = False,False,False else: print('Your input is illegal, please input again:') break else: print('The number is too small or large, please input again:') elif city_number == 'u': break elif city_number.lower() == 'q': print('You stopped the program!') indicater1,indicater2 = False, False else: print('The input is not integer or \'u\' or \'q\', please input again:') continue else: print('The number is too small or large, please input again:') else: if province_number.lower() == 'q': print('You stopped the program!') indicater1 = False else: print('The input is not integer or \'q\', please input again: ')以上的菜单数据data_dic直接在程序中给出,也可以保存在一个文本文件或者数据库汇总。可在此程序基础上扩展。
阅读全文
1 0
- python中用字典实现三级菜单
- Python下三级菜单实现
- Python:三级菜单
- Python-三级菜单
- python-三级菜单
- python-------------三级菜单
- js实现三级菜单
- 三级菜单的实现
- CSS实现三级菜单
- Python三级菜单大概思路
- Python练习之三级菜单
- Python笔记(五)三级菜单
- Python三级菜单初学->脑残版
- python三级菜单(优化版)
- Python小程序:三级菜单
- python编程实例-三级菜单
- python中用字典格式化字符串
- 三级级联下拉菜单实现
- java中ArrayList和LinkedList的区别
- CF 832D Misha, Grisha and Underground(Tree+lca)
- 不可错过的javascript迷你库
- 【poj】3126 Prime Path(bfs)
- 设置mysql递增主键的起始值
- python中用字典实现三级菜单
- js split方法针对单个"\"反斜杠
- java.lang.reflect.UndeclaredThrowableException
- 线段树 --- 适用于修改性的求区间问题 【单点修改】
- Hibernate学习笔记 -- day01 Hibernate介绍及入门案例环境搭建
- 常用的知识点纪录
- spring的基本知识
- python--文件操作(I/O)
- TFTP与FTP的比较