1. 程式人生 > >Python實例---三級菜單的實現[high]

Python實例---三級菜單的實現[high]

word 永遠 master con ice 實現 一個 rule elif

# version: python3.2.5
# author: ‘FTL1012‘
# time: 2017/12/7 09:16
menu = {
    ‘陜西‘: {
        ‘西安‘: {
            ‘未名區‘: [‘國美‘, ‘蘇寧‘, ‘京東‘],
            ‘無名區‘: [‘百度‘, ‘360 ‘, ‘搜狗‘],
            ‘優雅區‘: [‘騰訊‘, ‘默默‘, ‘訂訂‘]
        },
        ‘寶雞‘: {
            ‘進隊區‘: [‘word‘, ‘excel‘, ‘ppt‘],
            ‘挖第區‘: [‘華為‘, ‘錘子‘, ‘小米‘],
            ‘阿克區‘: [‘adidas‘, ‘unique‘, ‘masterbrown‘]
        },
        ‘漢中‘: {
            ‘呵呵區‘: [‘流浪‘, ‘excel‘, ‘ppt‘],
            ‘幻化區‘: [‘猥瑣‘, ‘發育‘, ‘別浪‘],
            ‘三只區‘: [‘後裔‘, ‘李白‘, ‘杜甫‘]
        },
    },
    ‘山東‘: {
        ‘山東市‘: {
            ‘惠普區‘: [‘華夏‘, ‘建設‘, ‘農行‘]
        }
    },
    ‘湖南‘: {
        ‘長沙‘: {
            ‘豆腐區‘: [‘鍵盤‘, ‘鼠標‘, ‘顯示器‘]
        }
    }
}

current_layer = menu # 用於動態循環
parent_lists = []  # 保存父級,最後一個元素永遠都是父親級別
while True:
    if len(current_layer) != 0:
        for k in current_layer:
            print(k)
    else:
        print("no")
    choice = input("請選擇:").strip()
    if 0 == len(choice):
        continue
    if choice in current_layer:
        # parent_layer = current_layer
        parent_lists.append(current_layer)
        current_layer = current_layer[choice]
    elif choice == ‘b‘:
        print("返回上一級目錄...")
        # for k in parent_layer:
        #     print(k)
        # current_layer = parent_layer 
        if parent_lists:
            current_layer = parent_lists.pop()     # pop會取出並刪除最後一個元素
        else:
            print("已經到達首頁啦...")
    else:
        print("輸入錯誤...")

Python實例---三級菜單的實現[high]