字典三级菜单之逐级添加内容
1.单次增加一级节点
db = { } v = input(">>>") db[v] = {} print(db)
2.无限循环的来增加一级节点
db = { } while True: v = input(">>>") db[v] = {} print(db)
3.完善
db = { "上海": {}, "北京":{ "昌平":{ "沙河":{}, "回龙观":{}, }, "朝阳":{}, "海淀":{}, } } path = [] while True: temp = db for item in path: temp = temp[item] print("当前可选的所有子节点:", list(temp.keys())) choice = input('1:添加节点;2:查看节点(b/q);\n >>>') if choice == "1": name = input("请输入要添加的节点名称:") temp[name] = {} elif choice == "2": name = input("请输入要查看的节点名称:") path.append(name) elif choice.lower() == "b": if path: #if path == True: path.pop() elif choice.lower() == "q": print(temp) #输出当前字典后再退出 break else: print("输入错误,请重新输入!")