作业概述:
写一个三级菜单
实现如下功能:
(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即可 代码:
#coding=utf-8 import sys 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]} #遍历一级菜单 def one_level_menu(): error_num=1 one_level_menu_dict={} #print('一级菜单列表:',list(China_map.keys())) for i in range(len(list(China_map.keys()))): one_level_menu_dict[i]=list(China_map.keys())[i] print ('一级菜单dict:',one_level_menu_dict) while 1: one_menu_choose=input('请选择Region代号;q:退出系统,b:返回上级菜单') if one_menu_choose == 'b': print('最顶层菜单,无法返回') elif one_menu_choose == 'q': sys.exit() elif one_menu_choose in (str(list(one_level_menu_dict.keys()))): #list(one_level_menu_dict.keys())取出one_level_menu_dict字典中的所有的key值形成列表 error_num=1 #输入错误后,如果再输入正确,那么错误数重新计算 two_level_menu(one_level_menu_dict,one_menu_choose) #调用二级菜单函数,显示二级菜单 elif one_menu_choose not in (str(list(one_level_menu_dict.keys()))): print('Region代码输入错误,重新输入,第%d次错误' %error_num) if error_num < 3: error_num=error_num+1 else: sys.exit() #显示二级菜单 def two_level_menu(one_level_menu_dict,one_menu_choose): error_num=1 two_level_menu_dict={} Region_name=one_level_menu_dict[int(one_menu_choose)] #区域名字 print ('选择的区域的名字:',Region_name) #print ('二级菜单list:',list(China_map[Region_name].keys())) for i in range(len(list(China_map[Region_name].keys()))): two_level_menu_dict[i]=list(China_map[Region_name].keys())[i] print ('二级菜单dict:',two_level_menu_dict) while 1: two_menu_choose=input('请选择Province代号;q:退出系统,b:返回上级菜单') if two_menu_choose == 'b': #输入b,退回上级菜单 #print('bbbb') one_level_menu() elif two_menu_choose == 'q': #输入q,退出程序 sys.exit() elif two_menu_choose in (str(list(two_level_menu_dict.keys()))): #list(one_level_menu_dict.keys())取出one_level_menu_dict字典中的所有的key值形成列表 error_num=1 #输入错误后,如果再输入正确,那么错误数重新计算 three_level_menu(one_level_menu_dict,two_level_menu_dict,Region_name,one_menu_choose,two_menu_choose) #调用三级菜单函数,显示三级菜单 elif two_menu_choose not in (str(list(two_level_menu_dict.keys()))): print('Province代码输入错误,重新输入,第%d次错误' %error_num) if error_num < 3: error_num=error_num+1 else: sys.exit() #显示三级菜单 def three_level_menu(one_level_menu_dict,two_level_menu_dict,Region_name,one_menu_choose,two_menu_choose): error_num=1 three_level_menu_dict={} Province_name=two_level_menu_dict[int(two_menu_choose)] #城市名字 print ('选择的省的名字:',Province_name) #print ('三级菜单list:',list(China_map[Region_name][City_name])) for i in range(len(list(China_map[Region_name][Province_name]))): three_level_menu_dict[i]=list(China_map[Region_name][Province_name])[i] print ('三级菜单dict:',three_level_menu_dict) while 1: three_menu_choose=input('请选择City-District代号;q:退出系统,b:返回上级菜单') if three_menu_choose == 'b': two_level_menu(one_level_menu_dict,one_menu_choose) elif three_menu_choose == 'q': sys.exit() elif three_menu_choose in (str(list(three_level_menu_dict.keys()))): 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) elif three_menu_choose not in (str(list(three_level_menu_dict.keys()))): print('City-District代码输入错误,重新输入,第%d次错误' %error_num) if error_num < 3: error_num=error_num+1 else: sys.exit() 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): error_num=1 print('美丽的%s欢迎你' %three_level_menu_dict[int(two_menu_choose)]) city_district_name=three_level_menu_dict[int(two_menu_choose)] total_population=city_district_info[three_level_menu_dict[int(two_menu_choose)]][0] area=city_district_info[three_level_menu_dict[int(two_menu_choose)]][1] msg='''%s简介信息: ----------------------- 总人口:%s 总面积:%d ----------End----------''' %(city_district_name,total_population,area) print(msg) last_menu_choose=input('请选择代号:q:退出系统,b:返回上级菜单') if last_menu_choose == 'q': sys.exit() elif last_menu_choose == 'b': three_level_menu(one_level_menu_dict,two_level_menu_dict,Region_name,one_menu_choose,two_menu_choose) else: last_again_menu_choose=input('代号输入错误,请选重新输入:q:退出系统,b:返回上级菜单') if __name__=='__main__': one_level_menu()