python3,5 利用字典实现三级菜单的功能

# Author Richard_Kong
# !/usr/bin/env python
# --*-- encoding:utf-8 --*--
"""
三级菜单:
1、打印省市县三级菜单
2、可以返回上一级
3、可以随时退出

"""
menu = {
    '山东':{
        '济南':{
            '历城区':{},
            '槐阴区':{},
            '章丘':{}
        },
        '聊城':{
            '神仙':{},
            '高唐':{},
            '冠县':{}
        },
        '青岛':{
            '黄岛':{},
            '城阳':{},
            '即墨':{}
        }
    },
    '浙江':{
        '杭州':{
            '西湖':{},
            '江干':{},
            '滨江':{},
            '余杭':{},
            '西溪':{}
        },
        '宁波':{
            '巨轮':{},
            '余姚':{}
        }

    }

}

current_layer = menu
# 将字典存入列表中
layer = [menu]
# print(current_layer)
# print(layer[0])
while True:
    for k in current_layer:
        print(k)
    choice = input(">>:").strip()
    if choice == 'break':
        break
    elif choice == 'back':
        current_layer = layer[-1] # 列表中存放 字典
    elif choice not in current_layer:
        continue
    else:
        layer.append(current_layer)
        current_layer = current_layer[choice]

猜你喜欢

转载自blog.csdn.net/kokodudu/article/details/81505458