路飞学城-Python开发集训-第3章

路飞学城-Python开发集训-第3章

  时间比较紧凑,生活还得继续,抓紧时间学习,看直播,看视频,匆匆忙忙写笔记,这是生活该有的节奏吗?

  可能很多人选择安逸,觉得现在很好,以后也会依旧。但是有些人就是不甘堕落,总想出人头地,哪怕比身边的人强一点,生活滋润一点点也可以,选择很重要,什么时间选择更重要,视频中老师提到二十几岁不做选择,三十多岁选择很难,人生或以定格。

  不过我还是觉得虽然早选择好,但是迟了也不能后悔。之前在一个论坛看到一个博主经常发帖子都会说到一句话“要么学!要么不学!学和不学之间没有中间值 不学就放弃,学就要去认真的学!    --致选择”。以后我也要沿用这句话来鼓励自己。

  这次直播讲解了三级菜单的多种实现方式,最主要的两种如下:

多级嵌套循环(普通青年版):

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}

# 普通版
while True:
    for k in menu:
        print(k)
    choice = input(">1").strip() #输入第一层
    if not choice:continue
    if choice in menu:
        while True:
            for k in menu[choice]:
                print(k)
            choice2 = input(">>2").strip() # 输入第二层
            if not choice2:continue
            if choice2 in menu[choice]:
                print(menu[choice][choice2])
                while True:
                    for k in menu[choice][choice2]:
                        print(k)
                    choice3 = input(">>>3").strip() #输入第三层
                    if not choice3:continue
                    if choice3 in menu[choice][choice2]:
                        print(menu[choice][choice2][choice3])
                        while True:
                            choice4 = input(">>>>4").strip() # 输入第四层
                            if not choice4:continue
                            if choice4 in menu[choice][choice2][choice3]:
                                    print(menu[choice][choice2][choice3][choice4])
                            elif choice4 == 'b':
                                break
                            elif choice4 == 'q':
                                exit()
                            else:
                                print('该节点不存在,请重新输入!!')
                    elif choice3 == 'b':
                        break
                    elif choice3 == 'q':
                        exit()
                    else:
                        print('该节点不存在,请重新输入!!')
            elif choice2 =='b':
                break
            elif choice2 == 'q':
                exit()
            else:
                print('该节占不存在,请重新输入!!')
    elif choice =='b' :
        print('已经回退到第一级啦!!')
    elif choice == 'q':
        exit()
    else:
        print('该节点不存在,请重新输入!!')

简介简化(装逼版):

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}

test = menu
Back = [] #列表用于存储上一层
while True:
    for i in test:
        print(i)
    choice = input(">>>选择城市(按B返回上一级,按E退出程序)").strip()
    if choice in test:
        Back.append(test)
        test = test[choice]
    elif choice == 'B':
        test = Back.pop() if len(Back) != 0 else print("------已经是第一层菜单了------")
    elif choice == 'E':
        exit()

可以看到两种版本差别很大,第一种代码重复率很大,效率很低,而且层层嵌套容易把人看懵逼了,第二种则比较简洁,便于理解。

原文地址:https://www.cnblogs.com/yaoqian/p/9236932.html