2.实现多级菜单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.实现多级菜单相关的知识,希望对你有一定的参考价值。
需求:
1.可依次选择进入各子菜单
2.在各个子菜单可以返回上一级菜单,可以退出.
3. 所需新知识点:列表,字典
流程图:
1 # -*- coding:utf-8 -*- 2 # Author: JACK ZHAO 3 4 # 作业3:多级菜单 5 # 三级菜单 6 # 可依次选择进入各子菜单 7 # 所需新知识点:列表,字典 8 9 mydict = { 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 ‘上城区‘:["清河坊","鼓楼","城隍阁景区"], 35 ‘下城区‘:["武林广场","司徒雷登故居","凤凰寺"], 36 ‘西湖区‘:["杭州西湖景区","雷峰塔","宋城"]}, 37 ‘宁波‘:{ 38 ‘海曙区‘:["天一藏书阁","海曙楼","天封塔"], 39 ‘江北区‘:["慈城古县城","保国寺","老外滩"], 40 ‘北仑区‘:["太白山","九峰山","洋沙山"] 41 } 42 }, 43 } 44 45 print("欢迎使用城市景点查询小工具".center(50,"#")) 46 while True: 47 for index,i in enumerate(mydict): #遍历字典一级菜单 48 print(index,i) 49 choice1 = input("请输入要查询省份的名称【q|退出】:") 50 if choice1 in mydict: 51 while True: 52 for index, i in enumerate(mydict[choice1]): #二级菜单 53 print(index, i) 54 choice2 = input("请输入要查询城市的名称【b|返回,q|退出】:") 55 if choice2 in mydict[choice1]: 56 while True: 57 for index,i in enumerate(mydict[choice1][choice2]): #三级菜单 58 print(index,i) 59 choice3 = input("请输入要查询市区的名称【b|返回,q|退出】:") 60 if choice3 in mydict[choice1][choice2]: 61 while True: 62 for index, i in enumerate(mydict[choice1][choice2][choice3]): #遍历列表 63 print(index, i) 64 choice4 = input("已经是最后一级菜单,【b|返回,q|退出】:") 65 if choice4 == "q": 66 print("你已退出,欢迎再次光临!") 67 exit() 68 elif choice4 == "b": 69 break 70 elif choice3 == "q": 71 print("你已退出,欢迎再次光临!") 72 exit() 73 elif choice3 == "b": 74 break 75 else: 76 print("你输入的市区\\033[31m%s\\033[0m不在列表内,请重新输入." % (choice3)) 77 elif choice2 == "q": 78 print("你已退出,欢迎再次光临!") 79 exit() 80 elif choice2 == "b": 81 break 82 else: 83 print("你输入的城市\\033[31m%s\\033[0m不在列表内,请重新输入." % (choice2)) 84 elif choice1 == "q": 85 print("你已退出,欢迎再次光临!") 86 exit() 87 else: 88 print("你输入的省份\\033[31m%s\\033[0m不在列表内,请重新输入." % (choice1))
以上是关于2.实现多级菜单的主要内容,如果未能解决你的问题,请参考以下文章