三级菜单的三个版本
1.這是我們通過一般方式進(jìn)行的,就是按照正常的思路來寫的,一步一步走
1 #_author:sangshijia 2 #date:2016/8/24 3 menu_map={ 4 "河南":{ 5 "鄭州":["新密","登封","中牟"], 6 "洛陽":["汝陽","伊川","嵩縣"], 7 "南陽":["西峽","方城","內(nèi)鄉(xiāng)"], 8 }, 9 "河北":{ 10 "保定":["順平","曲陽","徐水"], 11 "邢臺(tái)":["臨城","清河","新河"], 12 "邯鄲":["廣平","雞澤","肥鄉(xiāng)"], 13 }, 14 "安徽":{ 15 "合肥":["長豐","肥東","肥西"], 16 "毫州":["蒙城","利辛","渦陽"], 17 "滁州":["鳳陽","來安","定遠(yuǎn)"], 18 }, 19 20 } 21 flag=True 22 while True: 23 for key in menu_map: 24 print(key) 25 choice=input("1,請(qǐng)輸入省份,b返回上一級(jí)目錄,d刪除省份>>>:").strip() 26 if choice =="b": 27 break 28 print("返回上一級(jí)目錄") 29 if choice =="d": 30 choice_del=input("請(qǐng)輸入需要?jiǎng)h除的省份:").strip() 31 del menu_map[choice_del] 32 elif choice in menu_map: 33 while True: 34 for key2 in menu_map[choice]: 35 print(key2) 36 choice2 = input("2,請(qǐng)輸入市,b返回上一級(jí)目錄,d刪除市>>>:").strip() 37 if choice2 =="b": 38 break 39 print("返回上一級(jí)目錄") 40 if choice2 =="d": 41 choice_del = input("請(qǐng)輸入需要?jiǎng)h除的市:").strip() 42 del menu_map[choice][choice_del] 43 elif choice2 in menu_map[choice]: 44 while True: 45 for key3 in menu_map[choice][choice2]: 46 print(key3) 47 choice3=input("3,請(qǐng)輸入縣,b返回上一級(jí)目錄,d刪除縣>>>:").strip() 48 if choice3 =="b": 49 break 50 print("返回上一級(jí)目錄") 51 if choice3 =="d": 52 choice_del = input("請(qǐng)輸入需要?jiǎng)h除的縣:").strip() 53 del menu_map[choice][choice2][choice3] 54 elif choice3 in menu_map[choice][choice2]: 55 print("last level") 56 elif choice3 not in menu_map[choice][choice2]: 57 choice3 = input("請(qǐng)輸入添加的縣>>:").strip() 58 menu_map[choice][choice2][choice3]={} 59 elif choice2 not in menu_map[choice]: 60 choice2 = input("請(qǐng)輸入添加的市>>:").strip() 61 menu_map[choice][choice2] = {} 62 elif choice not in menu_map: 63 choice = input("請(qǐng)輸入添加的省>>:").strip() 64 menu_map[choice] = {}2.這是簡便后的,代碼少了很多,就是把上一部分的一些重復(fù)代碼 簡化而成的
1 #_author:sangshijia 2 #date:2016/8/26 3 menu={ 4 "河南":{ 5 "鄭州":["新密","登封","中牟"], 6 "洛陽":["汝陽","伊川","嵩縣"], 7 "南陽":["西峽","方城","內(nèi)鄉(xiāng)"], 8 }, 9 "河北":{ 10 "保定":["順平","曲陽","徐水"], 11 "邢臺(tái)":["臨城","清河","新河"], 12 "邯鄲":["廣平","雞澤","肥鄉(xiāng)"], 13 }, 14 "安徽":{ 15 "合肥":["長豐","肥東","肥西"], 16 "毫州":["蒙城","利辛","渦陽"], 17 "滁州":["鳳陽","來安","定遠(yuǎn)"], 18 }, 19 20 } 21 import sys 22 current_layer=menu 23 parent_layers=[] 24 while True: 25 for key in current_layer: 26 print(key) 27 choice=input(">>>:").strip() 28 if len(choice)==0:continue 29 if choice in current_layer: 30 parent_layers.append(current_layer) 31 current_layer=current_layer[choice] 32 elif choice=="b": 33 if parent_layers: 34 current_layer=parent_layers.pop() 35 else: 36 print("無此項(xiàng)")3.通過文件把menu_map中的內(nèi)容存儲(chǔ)到新建的一個(gè)文件aaa里面,這樣就可以把menu_map這一段內(nèi)容注釋掉,通過調(diào)用新文件aaa來執(zhí)行,同時(shí)呢,也熟悉了文件的一些操作
,同時(shí)也添加了增加和刪除這2個(gè)功能,但是我只能用老方法來實(shí)現(xiàn)文件的加入,
1 #_author:sangshijia 2 #date:2016/8/24 3 #f=open('menu_map',encoding='utf8') 4 # menu_map={ 5 # "河南":{ 6 # "鄭州":["新密","登封","中牟"], 7 # "洛陽":["汝陽","伊川","嵩縣"], 8 # "南陽":["西峽","方城","內(nèi)鄉(xiāng)"], 9 # }, 10 # "河北":{ 11 # "保定":["順平","曲陽","徐水"], 12 # "邢臺(tái)":["臨城","清河","新河"], 13 # "邯鄲":["廣平","雞澤","肥鄉(xiāng)"], 14 # }, 15 # "安徽":{ 16 # "合肥":["長豐","肥東","肥西"], 17 # "毫州":["蒙城","利辛","渦陽"], 18 # "滁州":["鳳陽","來安","定遠(yuǎn)"], 19 # }, 20 # 21 # } 22 # new_menu=str(menu_map) 23 # f=open("aaa","w",encoding='utf8') 24 # f.write(new_menu) 25 # f.close() 26 f=open("aaa","r",encoding='utf8') 27 menu_map=f.read() 28 menu_map=eval(menu_map) 29 flag=True 30 while True: 31 for key in menu_map: 32 print(key) 33 choice=input("1,請(qǐng)輸入省份,b表示返回上一級(jí)目錄,d表示刪除省份>>>:").strip() 34 if choice =="b": 35 break 36 print("返回上一級(jí)目錄") 37 if choice =="d": 38 choice_del=input("請(qǐng)輸入需要?jiǎng)h除的省份:").strip() 39 del menu_map[choice_del] 40 elif choice in menu_map: 41 while True: 42 for key2 in menu_map[choice]: 43 print(key2) 44 choice2 = input("2,請(qǐng)輸入市,b表示返回上一級(jí)目錄,d表示刪除市>>>:").strip() 45 if choice2 =="b": 46 break 47 print("返回上一級(jí)目錄") 48 if choice2 =="d": 49 choice_del = input("請(qǐng)輸入需要?jiǎng)h除的市:").strip() 50 del menu_map[choice][choice_del] 51 elif choice2 in menu_map[choice]: 52 while True: 53 for key3 in menu_map[choice][choice2]: 54 print(key3) 55 choice3=input("3,請(qǐng)輸入縣,b表示返回上一級(jí)目錄,d表示刪除縣>>>:").strip() 56 if choice3 =="b": 57 break 58 print("返回上一級(jí)目錄") 59 if choice3 =="d": 60 choice_del = input("請(qǐng)輸入需要?jiǎng)h除的縣:").strip() 61 del menu_map[choice][choice2][choice3] 62 elif choice3 in menu_map[choice][choice2]: 63 print("last level") 64 elif choice3 not in menu_map[choice][choice2]: 65 choice3 = input("請(qǐng)輸入添加的縣>>:").strip() 66 menu_map[choice][choice2][choice3]={} 67 elif choice2 not in menu_map[choice]: 68 choice2 = input("請(qǐng)輸入添加的市>>:").strip() 69 menu_map[choice][choice2] = {} 70 elif choice not in menu_map: 71 choice = input("請(qǐng)輸入添加的省>>:").strip() 72 menu_map[choice] = {}?
轉(zhuǎn)載于:https://www.cnblogs.com/mars527/p/5843763.html
總結(jié)
- 上一篇: IMPDP导入实例(oracle)
- 下一篇: 调研《构建之法》指导下的历届作品