1.这是我们通过一般方式进行的,就是按照正常的思路来写的,一步一步走
1 #_author:sangshijia 2 #date:2016/8/24 3 menu_map={ 4 "河南":{ 5 "郑州":["新密","登封","中牟"], 6 "洛阳":["汝阳","伊川","嵩县"], 7 "南阳":["西峡","方城","内乡"], 8 }, 9 "河北":{ 10 "保定":["顺平","曲阳","徐水"], 11 "邢台":["临城","清河","新河"], 12 "邯郸":["广平","鸡泽","肥乡"], 13 }, 14 "安徽":{ 15 "合肥":["长丰","肥东","肥西"], 16 "毫州":["蒙城","利辛","涡阳"], 17 "滁州":["凤阳","来安","定远"], 18 }, 19 20 } 21 flag=True 22 while True: 23 for key in menu_map: 24 print(key) 25 choice=input("1,请输入省份,b返回上一级目录,d删除省份>>>:").strip() 26 if choice =="b": 27 break 28 print("返回上一级目录") 29 if choice =="d": 30 choice_del=input("请输入需要删除的省份:").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,请输入市,b返回上一级目录,d删除市>>>:").strip() 37 if choice2 =="b": 38 break 39 print("返回上一级目录") 40 if choice2 =="d": 41 choice_del = input("请输入需要删除的市:").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,请输入县,b返回上一级目录,d删除县>>>:").strip() 48 if choice3 =="b": 49 break 50 print("返回上一级目录") 51 if choice3 =="d": 52 choice_del = input("请输入需要删除的县:").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("请输入添加的县>>:").strip() 58 menu_map[choice][choice2][choice3]={} 59 elif choice2 not in menu_map[choice]: 60 choice2 = input("请输入添加的市>>:").strip() 61 menu_map[choice][choice2] = {} 62 elif choice not in menu_map: 63 choice = input("请输入添加的省>>:").strip() 64 menu_map[choice] = {}
2.这是简便后的,代码少了很多,就是把上一部分的一些重复代码 简化而成的
1 #_author:sangshijia 2 #date:2016/8/26 3 menu={ 4 "河南":{ 5 "郑州":["新密","登封","中牟"], 6 "洛阳":["汝阳","伊川","嵩县"], 7 "南阳":["西峡","方城","内乡"], 8 }, 9 "河北":{ 10 "保定":["顺平","曲阳","徐水"], 11 "邢台":["临城","清河","新河"], 12 "邯郸":["广平","鸡泽","肥乡"], 13 }, 14 "安徽":{ 15 "合肥":["长丰","肥东","肥西"], 16 "毫州":["蒙城","利辛","涡阳"], 17 "滁州":["凤阳","来安","定远"], 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("无此项")
3.通过文件把menu_map中的内容存储到新建的一个文件aaa里面,这样就可以把menu_map这一段内容注释掉,通过调用新文件aaa来执行,同时呢,也熟悉了文件的一些操作
,同时也添加了增加和删除这2个功能,但是我只能用老方法来实现文件的加入,
1 #_author:sangshijia 2 #date:2016/8/24 3 #f=open('menu_map',encoding='utf8') 4 # menu_map={ 5 # "河南":{ 6 # "郑州":["新密","登封","中牟"], 7 # "洛阳":["汝阳","伊川","嵩县"], 8 # "南阳":["西峡","方城","内乡"], 9 # }, 10 # "河北":{ 11 # "保定":["顺平","曲阳","徐水"], 12 # "邢台":["临城","清河","新河"], 13 # "邯郸":["广平","鸡泽","肥乡"], 14 # }, 15 # "安徽":{ 16 # "合肥":["长丰","肥东","肥西"], 17 # "毫州":["蒙城","利辛","涡阳"], 18 # "滁州":["凤阳","来安","定远"], 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,请输入省份,b表示返回上一级目录,d表示删除省份>>>:").strip() 34 if choice =="b": 35 break 36 print("返回上一级目录") 37 if choice =="d": 38 choice_del=input("请输入需要删除的省份:").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,请输入市,b表示返回上一级目录,d表示删除市>>>:").strip() 45 if choice2 =="b": 46 break 47 print("返回上一级目录") 48 if choice2 =="d": 49 choice_del = input("请输入需要删除的市:").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,请输入县,b表示返回上一级目录,d表示删除县>>>:").strip() 56 if choice3 =="b": 57 break 58 print("返回上一级目录") 59 if choice3 =="d": 60 choice_del = input("请输入需要删除的县:").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("请输入添加的县>>:").strip() 66 menu_map[choice][choice2][choice3]={} 67 elif choice2 not in menu_map[choice]: 68 choice2 = input("请输入添加的市>>:").strip() 69 menu_map[choice][choice2] = {} 70 elif choice not in menu_map: 71 choice = input("请输入添加的省>>:").strip() 72 menu_map[choice] = {}