练习题2:添加字典节点

创造字典并添加字典子节点,附件功能:新增,查看,返回,退出
db = {}
path = []

while True:
    temp = db
    for tempa in path:
        temp = temp[tempa]
    print("当前节点为:", list(temp.keys()))

    inp1 = input("1:新增节点;2:查看节点;B:返回上一层;Q:退出。\n>>>")
    if inp1 == "1":
        newkey = input("请输入节点名称: \n>>>")
        if newkey in temp:
            print("节点已经存在!")
        else:
            temp[newkey] = {}
        print(temp)
    elif inp1 == "2":
        name = input("请输入节点名称: ")
        if name in temp:
            path.append(name)
        else:
            print("节点不存在!")
    elif inp1.lower() == "b":
        if path:
            path.pop()
    elif inp1.lower() == "q":
        break
    else:
        print("Please input right key !")

 

posted @ 2021-06-12 11:43  Python全栈开发  阅读(68)  评论(0)    收藏  举报