Python字典练习,三级菜单

编写省、市、县三层以上字典,实现下列要求:

1.进入到一层菜单就把下层菜单的所有子项显示出来

2.每一层都可以返回到上一层菜单中

3.每一层都可以退出程序

1》》》》方式一、多个循环完成练习

menu = {
    '北京':{
        '朝阳':{
            '国贸':{
                'CICC':{},
                'HP':{},
                '渣打银行':{},
                'CCTV':{},
            },
            '望京':{
                '陌陌':{},
                '奔驰':{},
                '360':{},
            },
            '三里屯':{
                '优衣库':{},
                'APPLE':{},
            }
        },
        '昌平':{
            "沙河":{
                "老男孩":{},
                "阿泰包子":{},
            },
            "天通苑": {
                '链家':{},
                "我爱我家":{},
            },
            "回龙观": {},
        },
        '海淀':{
            '五道口':{
                '谷歌':{},
                '网易':{},
                '搜狐':{},
                '搜狗': {},
                '快手': {},
            },
            '中关村':{
                '优酷': {},
                '爱奇艺': {},
                '汽车之家': {},
                '新东方': {},
                'QQ': {},
            },
        }
    },
    '上海':{
        '浦东':{
            '陆家嘴':{
                "CICC":{},
                '高盛': {},
                '摩根': {},
            },
            '外滩':{},
        },
        '闽兴':{},
        '静安':{},
    },
    '山东':{
        '济南':{

        },
        '德州':{
            '乐陵': {
                '老家': {},
            },
            '平原县': {},
        },
        '青岛':{},
    },
}
back_flag = False
exit_flag = False

#可以一层一层进入菜单,可以返回上一层,可以在任意层退出主菜单
while not back_flag and not exit_flag:
    for key in menu:
        print(key)
    choice = input('1>>').strip()#strip将前后的空格和换行去掉
    if choice == 'q':
        exit_flag = True
    if choice in menu:
        while not back_flag and not exit_flag:#让程序停留在第二层循环
            for key2 in menu[choice]:
                print(key2)
            choice2 = input('2>>>').strip()
            if choice2 == 'b':
                back_flag = True
            if choice2 == 'q':
                exit_flag = True
            if choice2 in menu[choice]:
                while not back_flag and not exit_flag:
                    for key3 in menu[choice][choice2]:
                        print(key3)
                    choice3 = input('3>>').strip()
                    if choice3 == 'b':
                        back_flag = True
                    if choice3 == 'q':
                        exit_flag = True
                    if choice3 in menu[choice][choice2]:
                        while not back_flag and not exit_flag:
                            for key4 in menu[choice][choice2][choice3]:
                                print(key4)
                            choice4 = input('4>>').strip()
                            if choice4 == 'b':
                                back_flag = True
                            if choice4 == 'q':
                                exit_flag = True
                        else:
                            back_flag = False
                else:
                    back_flag = False
        else:
            back_flag = False

2》》》》方式二、代码精简完成练习

# __author = 'LL'
# 2018/7/16 18:00
# PyCharm
#省略多个循环控制实现代码精简
menu = {
    '北京':{
        '朝阳':{
            '国贸':{
                'CICC':{},
                'HP':{},
                '渣打银行':{},
                'CCTV':{},
            },
            '望京':{
                '陌陌':{},
                '奔驰':{},
                '360':{},
            },
            '三里屯':{
                '优衣库':{},
                'APPLE':{},
            }
        },
        '昌平':{
            "沙河":{
                "老男孩":{},
                "阿泰包子":{},
            },
            "天通苑": {
                '链家':{},
                "我爱我家":{},
            },
            "回龙观": {},
        },
        '海淀':{
            '五道口':{
                '谷歌':{},
                '网易':{},
                '搜狐':{},
                '搜狗': {},
                '快手': {},
            },
            '中关村':{
                '优酷': {},
                '爱奇艺': {},
                '汽车之家': {},
                '新东方': {},
                'QQ': {},
            },
        }
    },
    '上海':{
        '浦东':{
            '陆家嘴':{
                "CICC":{},
                '高盛': {},
                '摩根': {},
            },
            '外滩':{},
        },
        '闽兴':{},
        '静安':{},
    },
    '山东':{
        '济南':{

        },
        '德州':{
            '乐陵': {
                '老家': {},
            },
            '平原县': {},
        },
        '青岛':{},
    },
}
current_layer = menu # 给字典赋值一个动态的项
parent_layers = []
while True:
    for key in current_layer:#每次遍历的是一个新的子项
        print(key)
    choice = input('>>>:').strip()
    if len(choice) == 0:continue#判断输入的值是否有效
    if choice in current_layer:
        parent_layers.append(current_layer)#将所有key值一层一层的放在列表中
        current_layer = current_layer[choice]
    elif choice == 'b':
        # pop(index)方法为删除并返回指定索引位置的项,不写参数,默认为删除最后一项并返回
        if parent_layers:#判断父亲层是否为空,不为空是True
            current_layer = parent_layers.pop()#取出最后一个值将该父亲层赋值给子层
    elif choice == 'q':
        break
    else:
        print('无此项,请重新输入下列选项:')

猜你喜欢

转载自blog.csdn.net/qq_35508162/article/details/81070025
今日推荐