三级菜单_要求:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 菜单数据保存在文件中
4. 让用户选择是否要退出
5. 有返回上一级菜单的功能
代码实现:
首先定义好数据源
1 __author__ = "Kim Zeng" 2 3 data = { 4 '北京': { 5 "昌平": { 6 "沙河": ["冰糖葫芦", "京酱肉丝"], 7 "天通苑": ["链家地产", "我爱我家"] 8 }, 9 "朝阳": { 10 "望京": ["奔驰", "奥迪"], 11 "国贸": {"百度", "HP"}, 12 "东直门": {"大街", "小米"}, 13 }, 14 "海淀": {"敏", "烤鸭"}, 15 }, 16 '山东': { 17 "德州": {"扒鸡"}, 18 "青岛": {"啤酒"}, 19 "济南": {"红枣"} 20 }, 21 '广东': { 22 "东莞": {"美女"}, 23 "深圳": {"汽车"}, 24 "佛山": {"照明"}, 25 } 26 }
实现功能:
1 exit_flag = False #设置一个标识位 2 while not exit_flag: 3 for i in data: 4 print(i) 5 choose = input("第一层>>>") 6 if choose == 'b': 7 print("不能再返回了>>>") 8 continue 9 elif choose == 'q': 10 exit_flag = True 11 elif choose not in data: 12 print("不存在,重新输入>>>") 13 continue 14 else: 15 while not exit_flag: 16 for i2 in data[choose]: 17 print("\t", i2) 18 choose2 = input("第二层>>>") 19 if choose2 == 'b': 20 break 21 elif choose2 == 'q': 22 exit_flag = True 23 elif choose2 not in data[choose]: 24 print("不存在,重新输入>>>") 25 continue 26 else: 27 while not exit_flag: 28 for i3 in data[choose][choose2]: 29 print("\t\t", i3) 30 choose3 = input("第三层>>>") 31 if choose3 == 'b': 32 break 33 elif choose3 == 'q': 34 exit_flag = True 35 elif choose3 not in data[choose][choose2]: 36 print("不存在,重新输入>>>") 37 continue 38 else: 39 while not exit_flag: 40 choose4 = input("最后了,b返回,q退出>>>") 41 if choose4 == 'b': 42 break 43 elif choose4 == 'q': 44 exit_flag = True 45 for i4 in data[choose][choose2]: 46 print("\t\t", i4)
标签:菜单,示例,flag,exit,choose,print,data,三级 From: https://www.cnblogs.com/zcest/p/17222626.html