前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >字典三级菜单之逐级添加内容

字典三级菜单之逐级添加内容

作者头像
py3study
发布2020-01-19 17:28:53
7190
发布2020-01-19 17:28:53
举报
文章被收录于专栏:python3python3python3

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("输入错误,请重新输入!")
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档