作业概述:
写一个三级菜单
实现如下功能:
(1)可依次选择进入各子菜单
(2)在每一级按“b”都可以返回上一级,按“q"都可以退出
流程图:
readme:
简介: three_menu.py是显示三级菜单的程序主要功能: 显示一级(区域)、二级(省)、三级菜单(市、区),三级菜单再选泽可以显示市区的人口和面积(为了节省时间,随意指定的),在任意级都可以返回上一级,或者退出 三级菜单和市区的人口和面积用字典表示: China_map={ ‘华北‘:{‘北京‘:[‘东城‘,‘西城‘,‘朝阳‘,‘海淀‘],‘河北‘:[‘保定‘,‘邯郸‘,‘唐山‘,‘海淀‘]}, ‘华东‘:{‘上海‘:[‘虹桥‘,‘徐汇‘,‘黄埔‘,‘普陀‘],‘江苏‘:[‘南京‘,‘苏州‘,‘无锡‘,‘徐州‘]}, ‘华南‘:{‘广东‘:[‘深圳‘,‘东莞‘,‘湛江‘,‘佛山‘],‘海南‘:[‘海口‘,‘三亚‘,‘三沙‘]} } city_district_info={‘东城‘:[1000,2000],‘西城‘:[1001,2001],‘朝阳‘:[1002,2002],‘海淀‘:[1003,2003],‘保定‘:[1004,2004],‘邯郸‘:[1005,2005], ‘唐山‘:[1006,2006],‘海淀‘:[1007,2007],‘虹桥‘:[1008,2008],‘徐汇‘:[1009,2009],‘黄埔‘:[1010,2010],‘黄埔‘:[1011,2011],‘南京‘:[1012,2012],‘苏州‘:[1013,2013], ‘无锡‘:[1014,2014],‘徐州‘:[1015,2015],‘深圳‘:[1016,2016],‘东莞‘:[1017,2017],‘湛江‘:[1018,2018],‘佛山‘:[1019,2019],‘海口‘:[1020,2020], ‘三亚‘:[1021,2021],‘三沙‘:[1022,2022]} 执行方法: 在cmd下进入three_menu.py文件的路径,然后直接执行python three_menu.py即可 代码:
1 #coding=utf-8 2 import sys 3 China_map={ 4 ‘华北‘:{‘北京‘:[‘东城‘,‘西城‘,‘朝阳‘,‘海淀‘],‘河北‘:[‘保定‘,‘邯郸‘,‘唐山‘,‘海淀‘]}, 5 ‘华东‘:{‘上海‘:[‘虹桥‘,‘徐汇‘,‘黄埔‘,‘普陀‘],‘江苏‘:[‘南京‘,‘苏州‘,‘无锡‘,‘徐州‘]}, 6 ‘华南‘:{‘广东‘:[‘深圳‘,‘东莞‘,‘湛江‘,‘佛山‘],‘海南‘:[‘海口‘,‘三亚‘,‘三沙‘]} 7 } 8 city_district_info={‘东城‘:[1000,2000],‘西城‘:[1001,2001],‘朝阳‘:[1002,2002],‘海淀‘:[1003,2003],‘保定‘:[1004,2004],‘邯郸‘:[1005,2005], 9 ‘唐山‘:[1006,2006],‘海淀‘:[1007,2007],‘虹桥‘:[1008,2008],‘徐汇‘:[1009,2009],‘黄埔‘:[1010,2010],‘黄埔‘:[1011,2011],‘南京‘:[1012,2012],‘苏州‘:[1013,2013], 10 ‘无锡‘:[1014,2014],‘徐州‘:[1015,2015],‘深圳‘:[1016,2016],‘东莞‘:[1017,2017],‘湛江‘:[1018,2018],‘佛山‘:[1019,2019],‘海口‘:[1020,2020], 11 ‘三亚‘:[1021,2021],‘三沙‘:[1022,2022]} 12 13 #遍历一级菜单 14 def one_level_menu(): 15 error_num=1 16 one_level_menu_dict={} 17 #print(‘一级菜单列表:‘,list(China_map.keys())) 18 19 for i in range(len(list(China_map.keys()))): 20 one_level_menu_dict[i]=list(China_map.keys())[i] 21 print (‘一级菜单dict:‘,one_level_menu_dict) 22 23 while 1: 24 one_menu_choose=input(‘请选择Region代号;q:退出系统,b:返回上级菜单‘) 25 if one_menu_choose == ‘b‘: 26 print(‘最顶层菜单,无法返回‘) 27 elif one_menu_choose == ‘q‘: 28 sys.exit() 29 elif one_menu_choose in (str(list(one_level_menu_dict.keys()))): #list(one_level_menu_dict.keys())取出one_level_menu_dict字典中的所有的key值形成列表 30 error_num=1 #输入错误后,如果再输入正确,那么错误数重新计算 31 two_level_menu(one_level_menu_dict,one_menu_choose) #调用二级菜单函数,显示二级菜单 32 elif one_menu_choose not in (str(list(one_level_menu_dict.keys()))): 33 print(‘Region代码输入错误,重新输入,第%d次错误‘ %error_num) 34 if error_num < 3: 35 error_num=error_num+1 36 else: 37 sys.exit() 38 39 #显示二级菜单 40 def two_level_menu(one_level_menu_dict,one_menu_choose): 41 error_num=1 42 two_level_menu_dict={} 43 Region_name=one_level_menu_dict[int(one_menu_choose)] #区域名字 44 print (‘选择的区域的名字:‘,Region_name) 45 #print (‘二级菜单list:‘,list(China_map[Region_name].keys())) 46 47 for i in range(len(list(China_map[Region_name].keys()))): 48 two_level_menu_dict[i]=list(China_map[Region_name].keys())[i] 49 print (‘二级菜单dict:‘,two_level_menu_dict) 50 51 while 1: 52 two_menu_choose=input(‘请选择Province代号;q:退出系统,b:返回上级菜单‘) 53 if two_menu_choose == ‘b‘: #输入b,退回上级菜单 54 #print(‘bbbb‘) 55 one_level_menu() 56 elif two_menu_choose == ‘q‘: #输入q,退出程序 57 sys.exit() 58 elif two_menu_choose in (str(list(two_level_menu_dict.keys()))): #list(one_level_menu_dict.keys())取出one_level_menu_dict字典中的所有的key值形成列表 59 error_num=1 #输入错误后,如果再输入正确,那么错误数重新计算 60 three_level_menu(one_level_menu_dict,two_level_menu_dict,Region_name,one_menu_choose,two_menu_choose) #调用三级菜单函数,显示三级菜单 61 elif two_menu_choose not in (str(list(two_level_menu_dict.keys()))): 62 print(‘Province代码输入错误,重新输入,第%d次错误‘ %error_num) 63 if error_num < 3: 64 error_num=error_num+1 65 else: 66 sys.exit() 67 68 #显示三级菜单 69 def three_level_menu(one_level_menu_dict,two_level_menu_dict,Region_name,one_menu_choose,two_menu_choose): 70 error_num=1 71 three_level_menu_dict={} 72 Province_name=two_level_menu_dict[int(two_menu_choose)] #城市名字 73 print (‘选择的省的名字:‘,Province_name) 74 #print (‘三级菜单list:‘,list(China_map[Region_name][City_name])) 75 76 for i in range(len(list(China_map[Region_name][Province_name]))): 77 three_level_menu_dict[i]=list(China_map[Region_name][Province_name])[i] 78 print (‘三级菜单dict:‘,three_level_menu_dict) 79 80 while 1: 81 three_menu_choose=input(‘请选择City-District代号;q:退出系统,b:返回上级菜单‘) 82 if three_menu_choose == ‘b‘: 83 two_level_menu(one_level_menu_dict,one_menu_choose) 84 elif three_menu_choose == ‘q‘: 85 sys.exit() 86 elif three_menu_choose in (str(list(three_level_menu_dict.keys()))): 87 display_city_distrct_info(one_level_menu_dict,two_level_menu_dict,three_level_menu_dict,Region_name,one_menu_choose,two_menu_choose,city_district_info) 88 elif three_menu_choose not in (str(list(three_level_menu_dict.keys()))): 89 print(‘City-District代码输入错误,重新输入,第%d次错误‘ %error_num) 90 if error_num < 3: 91 error_num=error_num+1 92 else: 93 sys.exit() 94 95 96 def display_city_distrct_info(one_level_menu_dict,two_level_menu_dict,three_level_menu_dict,Region_name,one_menu_choose,two_menu_choose,city_district_info): 97 error_num=1 98 print(‘美丽的%s欢迎你‘ %three_level_menu_dict[int(two_menu_choose)]) 99 city_district_name=three_level_menu_dict[int(two_menu_choose)] 100 total_population=city_district_info[three_level_menu_dict[int(two_menu_choose)]][0] 101 area=city_district_info[three_level_menu_dict[int(two_menu_choose)]][1] 102 msg=‘‘‘%s简介信息: 103 ----------------------- 104 总人口:%s 105 总面积:%d 106 ----------End----------‘‘‘ %(city_district_name,total_population,area) 107 print(msg) 108 109 last_menu_choose=input(‘请选择代号:q:退出系统,b:返回上级菜单‘) 110 if last_menu_choose == ‘q‘: 111 sys.exit() 112 elif last_menu_choose == ‘b‘: 113 three_level_menu(one_level_menu_dict,two_level_menu_dict,Region_name,one_menu_choose,two_menu_choose) 114 else: 115 last_again_menu_choose=input(‘代号输入错误,请选重新输入:q:退出系统,b:返回上级菜单‘) 116 117 118 if __name__==‘__main__‘: 119 one_level_menu()
时间: 2024-09-30 20:24:58