三級菜單

打印三級菜單


menu = {
    '北京':{
        '朝陽':{},
        '望京':{},
        '三里屯':{},

        '昌平':{},
        '海錠':{
            "五道口":{
                "谷歌":{},
                "網易":{},
                "快手":{},
            }
        }
    },
    '上海':{},
}

current_layer = menu        #實現動態循環

parent_list = []            #保存所有父級,最後一個元素永遠是父級
while True:
    for key in current_layer:
        print(key)
    choice = input(">>>").strip()   #.strip去表符、空格
    if len(choice) == 0:continue    #判斷是否爲空
    if choice in current_layer:

        parent_list.append(current_layer)   #在進入下一層前,把當前層追加到列表裏
        current_layer = current_layer[choice]   #改爲子層
    elif choice == "b":
        if parent_list:
            current_layer = parent_list.pop()   #刪除列表裏最後一個元素並返回給變量
    elif choice == "q":
        break

    else:
        print("沒有此選項,請重新選擇")

輸出結果:

三級菜單

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