zoukankan      html  css  js  c++  java
  • Python进阶实战之三级菜单

    一、Python进阶实战之三级菜单

    1. 打印省、市、县三级菜单

    2. 可返回上一级

    3. 可随时退出程序

    1.1 面条版

    menu = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'google': {}
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    '百度': {},
                },
            },
            '昌平': {
                '沙河': {
                    '老男孩': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            '闵行': {
                "人民广场": {
                    '炸鸡店': {}
                }
            },
            '闸北': {
                '火车战': {
                    '携程': {}
                }
            },
            '浦东': {},
        },
        '山东': {},
    }
    
    tag = True
    while tag:
        menu1 = menu
        for key in menu1:  # 打印第一层
            print(key)
    
        choice1 = input('第一层>>: ').strip()  # 选择第一层
    
        if choice1 == 'b':  # 输入b,则返回上一级
            break
        if choice1 == 'q':  # 输入q,则退出整体
            tag = False
            continue
        if choice1 not in menu1:  # 输入内容不在menu1内,则继续输入
            continue
    
        while tag:
            menu_2 = menu1[choice1]  # 拿到choice1对应的一层字典
            for key in menu_2:
                print(key)
    
            choice2 = input('第二层>>: ').strip()
    
            if choice2 == 'b':
                break
            if choice2 == 'q':
                tag = False
                continue
            if choice2 not in menu_2:
                continue
    
            while tag:
                menu_3 = menu_2[choice2]
                for key in menu_3:
                    print(key)
    
                choice3 = input('第三层>>: ').strip()
                if choice3 == 'b':
                    break
                if choice3 == 'q':
                    tag = False
                    continue
                if choice3 not in menu_3:
                    continue
    
                while tag:
                    menu_4 = menu_3[choice3]
                    for key in menu_4:
                        print(key)
    
                    choice4 = input('第四层>>: ').strip()
                    if choice4 == 'b':
                        break
                    if choice4 == 'q':
                        tag = False
                        continue
                    if choice4 not in menu_4:
                        continue
    
                    # 第四层内没数据了,无需进入下一层
    
    北京
    上海
    山东
    第一层>>: 背景
    北京
    上海
    山东
    第一层>>: 北京
    海淀
    昌平
    朝阳
    东城
    第二层>>: 海淀
    五道口
    中关村
    上地
    第三层>>: 中关村
    爱奇艺
    汽车之家
    youku
    第四层>>: q
    

    1.2 文艺青年版

    menu = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'google': {}
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    '百度': {},
                },
            },
            '昌平': {
                '沙河': {
                    '老男孩': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            '闵行': {
                "人民广场": {
                    '炸鸡店': {}
                }
            },
            '闸北': {
                '火车战': {
                    '携程': {}
                }
            },
            '浦东': {},
        },
        '山东': {},
    }
    
    # part1(初步实现):能够一层一层进入
    layers = [
        menu,
    ]
    
    while True:
        current_layer = layers[-1]
        for key in current_layer:
            print(key)
    
        choice = input('>>: ').strip()
        
        if choice == 'q':
            break
    
        if choice not in current_layer: continue
    
        layers.append(current_layer[choice])
    
    # part2(改进):加上退出机制
    layers = [
        menu,
    ]
    
    while True:
        if len(layers) == 0: break
        current_layer = layers[-1]
        for key in current_layer:
            print(key)
    
        choice = input('>>: ').strip()
    
        if choice == 'b':
            layers.pop(-1)
            continue
        if choice == 'q': break
    
        if choice not in current_layer: continue
    
        layers.append(current_layer[choice])
    
    北京
    上海
    山东
    >>: 上海
    闵行
    闸北
    浦东
    >>: 浦东
    >>: q
    北京
    上海
    山东
    >>: q
  • 相关阅读:
    Mysql数据库快速备份还原-mysqldump
    写给年轻人的交友和人脉建议
    令人担忧的趋势:科技崇拜与人文失落
    高情商的特征
    高情商与朋友圈
    数据库临时表空间设置
    oracle 临时表空间的增删改查
    语言表达能力写作能力决定一个人的发展和未来
    一个人如何从平庸到优秀,再到卓越?
    06.堆排序
  • 原文地址:https://www.cnblogs.com/nickchen121/p/11069998.html
Copyright © 2011-2022 走看看