1 #-*- coding:utf-8 -*- 2 3 ‘‘‘******************************************************‘ 4 三级菜单 5 1. 运行程序输出第一级菜单 6 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 7 3. 返回上一级菜单和顶部菜单 8 4. 菜单数据保存在文件中" 9 ‘******************************************************‘‘‘ 10 11 import sys 12 dic = { 13 "北京":{ 14 "fa":{ 15 "aa":[123] 16 }, 17 "li":{ 18 "bb":[122,332] 19 } 20 }, 21 "上海": { 22 "浦东新区":{ 23 "金桥":["新金桥路","云桥路"] 24 }, 25 "普陀":{ 26 "dd":["s"] 27 } 28 } 29 } 30 31 32 file = open("menu.txt","w+",encoding=‘utf-8‘) 33 34 lTmp = [] 35 while True: 36 dTmp = dic 37 for item in lTmp: 38 if dTmp.get(item)!= None: 39 dTmp = dTmp[item] 40 else: 41 print("您要查找的菜单:%s不存在!"%lTmp[-1]) 42 lTmp.pop() 43 print("当前的菜单项有:", list(dTmp.keys())) 44 choice = input("1、查看菜单节点,2、返回上一级(b/B),返回顶级菜单(t\T) 3、退出(q/Q)\n>>>") 45 if choice == ‘1‘: 46 name = input("请输入您要查看的菜单\n>>>") 47 lTmp.append(name) 48 elif choice.lower() == ‘b‘ or choice.lower() == ‘t‘: 49 if choice.lower() == ‘b‘: 50 lTmp.pop() 51 else: 52 lTmp.clear() 53 elif choice.lower() == ‘q‘: 54 file.write(str(dic)) 55 file.close() 56 sys.exit(1) 57 else: 58 print("很抱歉您的输入有误,请重新输入!")
时间: 2024-10-07 23:05:08