python之三级菜单
要求:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 菜单数据保存在文件中
4. 让用户选择是否要退出
5. 有返回上一级菜单的功能
流程图:
代码:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 district_maps = { #定义字典 6 "华东区":{"山东":["济南","青岛","淄博"],"江苏":["苏州","南京","无锡"],"浙江":["杭州","宁波","温州"],}, 7 "华南区":{"广东":["广州","深圳","珠海"],"广西":["桂林","柳州","北海"],"海南":["三亚","海口","三沙"]}, 8 "华中区":{"湖北":["武汉","宜昌","黄冈"],"湖南":["长沙","株洲","湘潭"],"江西":["南昌","九江","上饶"]}, 9 "华北区":{"北京":["东城区","朝阳区","海淀区"],"天津":["和平区","河西区","河北区"],"河北":["石家庄","唐山","张家口"]}, 10 "西北区":{"宁夏":["银川","吴忠","固原"],"青海":["西宁","海东","海北藏族自治州"],"陕西":["西安","咸阳","延安"]}, 11 "西南区":{"四川":["成都","绵阳","广元"],"云南":["昆明","丽江","玉溪"],"贵州":["贵阳","安顺","铜仁"]}, 12 "东北区":{"辽宁":["沈阳","大连","鞍山"],"吉林":["长春","吉林","辽源"],"黑龙江":["哈尔滨","齐齐哈尔","牡丹江"]}, 13 "港澳台区":{"香港":["香港岛","九龙半岛","新界"],"澳门":["澳门半岛","离岛","路氹城"],"台湾":["台北","台中","台南"]}, 14 } 15 16 def menu(): 17 while True: 18 on = () 19 c = () 20 for i in district_maps: 21 print(i) 22 on = input("请输入地区:") 23 while True: 24 if on in district_maps.keys(): 25 for e in district_maps[on]: 26 print(e) 27 c = input("请输入省份:") 28 if c in district_maps[on]: 29 for d in district_maps[on][c]: 30 print(d) 31 while True: 32 d = input("返回上层请输入b,退出请输入q:") 33 if d == "b": 34 break 35 elif d == "q": 36 print("谢谢使用") 37 exit(0) 38 else: 39 print("输入错误,请重新输入") 40 elif c == "b": 41 break 42 elif c == "q": 43 print("谢谢使用") 44 exit(0) 45 else: 46 print("输入有误,请重新输入") 47 elif on == "q": 48 print("谢谢使用") 49 exit(0) 50 else: 51 print("输入有误,请重新输入") 52 if __name__ == '__main__': 53 menu()