代碼練習,三級菜單

1


# -*- coding:utf-8 -*-

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '網易': {},
                'google': {}
            },
            '中關村': {
                '愛奇藝': {},
                '汽車之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龍觀': {},
        },
        '朝陽': {},
        '東城': {},
    },
    '上海': {
        '閔行': {
            "人民廣場": {
                '炸雞店': {}
            }
        },
        '閘北': {
            '火車站': {
                '攜程': {}
            }
        },
        '浦東': {},
    },
    '山東': {},
}

prompt = "請輸入菜單名,進入子菜單\n 輸入'b'回到上級菜單\n 輸入'q'退出程序:"
current_menu = menu  # 當前菜單
parent_menu = []  # 父菜單
while True:
    if len(current_menu) == 0:
        print('當前菜單爲最底層')
    for i in current_menu:
        print('菜單-->', i)
    choice = input(prompt).strip()
    if not choice: continue
    if choice in current_menu:
        parent_menu.append(current_menu)  # 記錄父菜單列表
        current_menu = current_menu[choice]  # 當前菜單變爲原菜單的子菜單
    elif choice == 'b':
        if len(parent_menu) != 0:
            current_menu = parent_menu.pop()  # 此命令刪除最後一個列表元素並返回值給當前菜單 。
        else:
            print('已到菜單最頂層')
    elif choice == 'q':
        print('退出程序')
        break
    else:
        print('請重新輸入')


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章