python字典練習:用戶查找和添加地區
阿新 • • 發佈:2018-10-02
print bsp pri lis 存在 查找 emp {} ()
1 db = {} # 全部地區 2 path = [] # 當前查看的路徑["長沙","芙蓉區"] 3 while True: 4 temp = db 5 for item in path: 6 temp = temp[item] 7 print("可以選地區:", list(temp.keys())) 8 # 用戶輸入命令: 9 com = input("1:添加地區,2:選擇查看地區,3:返回上一層\n>>>") 10 if com == "1": 11 com = input("請輸入要添加的地區:\n>>>>") 12 if com in temp: 13 print("該地區已存在") 14 else: 15 temp[com] = {} 16 elif com == "2": 17 com = input("請輸入要查看的地區:\n>>>>") 18 if com in temp: 19 path.append(com) 20 else: 21 print("沒有該地區!") 22 elif com == "3": 23 if path: 24 path.pop() 25 else: 26 print("輸入不合法!")
python字典練習:用戶查找和添加地區