python 打印三级菜单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 打印三级菜单相关的知识,希望对你有一定的参考价值。
刚刚学习python,发现很多基础理论都能懂,但是要写点东西,就脑袋一片空白,后面看了网友的文章才慢慢写出来 ......
原文:http://www.cnblogs.com/pyramid1001/p/5803294.html
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # __author__ = cjx 4 import sys 5 6 china_map = { 7 ‘山东‘: { 8 ‘青岛‘: [‘四方‘, ‘黄岛‘, ‘崂山‘, ‘李沧‘, ‘城阳‘], 9 ‘济南‘: [‘历城‘, ‘槐荫‘, ‘高新‘, ‘长青‘, ‘章丘‘], 10 ‘烟台‘: [‘龙口‘, ‘莱山‘, ‘牟平‘, ‘蓬莱‘, ‘招远‘] 11 }, 12 ‘江苏‘: { 13 ‘苏州‘: [‘沧浪‘, ‘相城‘, ‘平江‘, ‘吴中‘, ‘昆山‘], 14 ‘南京‘: [‘白下‘, ‘秦淮‘, ‘浦口‘, ‘栖霞‘, ‘江宁‘], 15 ‘无锡‘: [‘崇安‘, ‘南长‘, ‘北塘‘, ‘锡山‘, ‘江阴‘] 16 }, 17 ‘浙江‘: { 18 ‘杭州‘: [‘西湖‘, ‘江干‘, ‘下城‘, ‘上城‘, ‘滨江‘], 19 ‘宁波‘: [‘海曙‘, ‘江东‘, ‘江北‘, ‘镇海‘, ‘余姚‘], 20 ‘温州‘: [‘鹿城‘, ‘龙湾‘, ‘乐清‘, ‘瑞安‘, ‘永嘉‘] 21 }, 22 ‘安徽‘: { 23 ‘合肥‘: [‘蜀山‘, ‘庐阳‘, ‘包河‘, ‘经开‘, ‘新站‘], 24 ‘芜湖‘: [‘镜湖‘, ‘鸠江‘, ‘无为‘, ‘三山‘, ‘南陵‘], 25 ‘蚌埠‘: [‘蚌山‘, ‘龙子湖‘, ‘淮上‘, ‘怀远‘, ‘固镇‘] 26 }, 27 ‘广东‘: { 28 ‘深圳‘: [‘罗湖‘, ‘福田‘, ‘南山‘, ‘宝安‘, ‘布吉‘], 29 ‘广州‘: [‘天河‘, ‘珠海‘, ‘越秀‘, ‘白云‘, ‘黄埔‘], 30 ‘东莞‘: [‘莞城‘, ‘长安‘, ‘虎门‘, ‘万江‘, ‘大朗‘] 31 } 32 } 33 34 province_list = list(china_map.keys()) 35 # print(province_list) 36 37 while True: 38 print(‘ 省 ‘.center(50, ‘*‘)) 39 for i,p in enumerate(province_list): 40 print(i+1,p) 41 # break 42 p_id = input(‘请输入要查看的省名称编号,退出请输入 q:‘) 43 if p_id.isdigit(): 44 p_id = int(p_id) 45 if p_id > 0 and p_id <= len(province_list): 46 pro_name = province_list[p_id - 1] 47 city_list = list(china_map[pro_name].keys()) 48 while True: 49 print(‘ 市 ‘.center(50, ‘*‘)) 50 for i, c in enumerate(city_list): 51 print(i+1, c) 52 c_id = input(‘请输入你要查看的市名称编号,返回上一级请输入 b,退出请输入 q :‘) 53 if c_id.isdigit(): 54 c_id = int(c_id) 55 if 0 < c_id <= len(city_list): 56 city_name = city_list[c_id-1] 57 town_list = china_map[pro_name][city_name] 58 while True: 59 print(‘ 县 ‘.center(50, ‘*‘)) 60 for i,t in enumerate(town_list): 61 print(i+1,t) 62 b_or_q = input(‘已经到最后一层,返回上一级请输入 b,退出请输入 q :‘) 63 if b_or_q == ‘b‘: 64 break 65 elif b_or_q == ‘q‘: 66 exit(0) 67 else: 68 print(‘输入有误!‘) 69 else: 70 print(‘您输入的ID %d 不存在!‘ % c_id) 71 elif c_id == ‘b‘: 72 break 73 elif c_id == ‘q‘: 74 sys.exit(‘正在退出 ......‘) 75 else: 76 print(‘无效输入‘) 77 78 else: 79 print(‘您输入的ID %d 不存在!‘ % p_id) 80 elif p_id == ‘q‘: 81 exit() 82 else: 83 print(‘无法识别您的输入‘)
以上是关于python 打印三级菜单的主要内容,如果未能解决你的问题,请参考以下文章