python三级菜单例子

学习pyhon,老男孩教学题目,实现三级菜单的切换

info = {
    "中国":{
        '北京':{
            "朝阳":["001","002"]
        },
        '上海':{
            "徐汇":["003","004"]
        },
        '广州':{
            "惠州":["005","006"]
        },

    },
}

while True:
    for i in info:
        print(i)
    one = input("第一级菜单-请选择【q 退出】:")
    if one in info:
        while True:
            for io in info[one]:
                print(io)

            one2 = input("第二级菜单-请选择【b 返回,q 退出】:")
            if one2 in info[one]:
                while True:
                    for oi in info[one][one2]:
                        print(oi)

                    one3 = input("第三级菜单-请选择【b 返回,q 退出】:")
                    if one3 in info[one][one2]:
                        while True:
                            for iu in info[one][one2][one3]:
                                print(iu)

                            one4 = input("最后一级菜单,【b 返回,q 退出】:")
                            if one4 == 'b':
                                break
                            else:
                                exit()

                    elif one3 == 'b':
                        break
                    elif one3 == 'q':
                        exit()

            elif one2 == 'b':
                break
            elif one2 == 'q':
                exit()

    elif one == 'q':
        exit()
    else:
        print("\033[31m请输入正确的!\033[0m\n")
分享