首页 » 编程 » Python基础 » 正文

三级菜单优化

# 需求:三级菜单,b返回上级,q退出

provice = {
    "四川省": {
        "成都市": {
            "金牛区": {
                "茶店子": {},
                "交大路": {}
            },
            "青羊区": {
                "光华村": {},
                "杜甫草堂": {}
            },
            "锦江区": {
                "春熙路": {}
            },
            "成华区": {
                "驷马桥": {}
            },
            "武侯区": {
                "高升桥": {},
                "红牌楼": {}
            },
            "高新区": {
                "天府软件园": {},
                "腾讯": {},
                "IBM": {}
            }
        },
        "宜宾市": {
            "翠屏区": {},
            "长宁县": {},
            "南溪区": {},
            "江安县": {}
        }
    },
    "北京市": {
        "东城区": {

        },
        "朝阳区": {

        }
    }
}

last = [provice]
current = provice

while True:
    for i in current:
        print(i)
    choice = input("input:").strip()
    if len(choice) == 0:
        continue
    if choice in current:
        last.append(current)
        current = current[choice]
    if choice == "b":
        if last:
            current = last[-1]
            last.pop()
    if choice == "q":
        break


发表评论

验证码加载中....