关于三级的菜单的尝试
Posted tianqianlan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于三级的菜单的尝试相关的知识,希望对你有一定的参考价值。
{ ‘安徽‘:{ "马鞍山":{ "秀山区":["地产","草地"], "雨山区":["代码","程序员"] }, "宣城":{ "泾县":["小山","大山"], "和县":{"桃花","山区"}, "郎溪县":{"地域","人文"}, }, "芜湖":{ "芜湖县":["小吃","东西"], "无为县":["无畏","繁荣"] "南岭县":["梨花","杏花"] }, }, "上海":{ "闵行":{}, "浦东":{}, "宝山":{} }, ‘浙江‘:{ "杭州":{}, "绍兴":{}, "义乌":{}, }, }
以上是文件内容,保存为文本文档
1 import sys 2 3 with open("weizhi.txt") as file: 4 f = file.read() 5 data = eval(f) 6 7 def list(): #展示第一层数据 8 for key in data.keys(): 9 print(key) 10 11 def choose_one(choose1): 12 while choose1 != ‘q‘: 13 global one 14 one = choose1 15 if choose1 in data: 16 for key2 in data[one]: 17 print(key2) 18 choose_two(input("输入第二级菜单")) 19 break 20 elif choose1 == "b": 21 print("这为一级菜单无法返回") 22 break 23 else: 24 error = input("你输入错误,请重新输入") 25 if error == "q": 26 break 27 else: 28 choose_one(error) 29 else: 30 sys.exit() 31 32 33 def choose_two(choose2): 34 while choose2 != ‘q‘: 35 global two 36 two = choose2 37 if choose2 in data[one]: 38 for key3 in data[one][two].keys(): 39 print(key3) 40 choose_three(input("请输入第三级菜单")) 41 break 42 elif two == ‘b‘: 43 choose_one(one) 44 break 45 else: 46 error = input("输入错误,重新输入") 47 if error == ‘q‘: 48 # sys.exit() 49 break 50 elif error == ‘b‘: 51 choose_one() 52 else: 53 choose_two(error) 54 else: 55 sys.exit() 56 57 58 def choose_three(choose3): 59 while choose3 != ‘q‘: 60 global three 61 three = choose3 62 if three in data[one][two]: 63 for key4 in data[one][two][three]: 64 print(key4) 65 choose_four(input("输入字母")) 66 break 67 elif three == ‘b‘: 68 choose_two(two) 69 break 70 else: 71 error = input("重新输入") 72 if error == ‘q‘: 73 break 74 else: 75 choose_three(error) 76 else: 77 sys.exit() 78 79 80 def choose_four(choose4): 81 if choose4 == ‘b‘: 82 choose_three(three) 83 elif choose4 == ‘q‘: 84 sys.exit() 85 else: 86 print("你输入错误: ") 87 think = input("是否想继续(y/n): ") 88 if think == ‘y‘: 89 list() 90 choose1 = input("一级菜单: ") 91 choose_one(choose1) 92 else: 93 sys.exit() 94 95 96 print("q表示推出,b表示返回上一级菜单") 97 list() 98 choose_one(input("请输入一级菜单"))
以上是关于关于三级的菜单的尝试的主要内容,如果未能解决你的问题,请参考以下文章