天天看點

列印三級菜單:一級一級執行——python基礎

程式 :三級菜單

要求:

列印省 市 縣 三級菜單

可以傳回上一級可以随時退出

傻瓜版

# menu = {
#     "北京":{
#         "海澱":{
#             "五道口":{
#                 "搜狐":{},
#                 "網易":{},
#                 "Google":{},
#             },
#             "上地":{
#                 "百度":{},
#             },
#             "中關村":{
#                 "愛奇藝":{},
#                 "汽車之家":{},
#                 "優酷":{},
#             },
#         },
#         "昌平":{
#             "沙河":{},
#             "天通苑":{},
#             "回龍觀":{},
#         },
#         "朝陽":{},
#     },
#     "上海":{},
#     "山東":{},
# }
#
# a = True
# back = True
# while a and back:
#     for  key  in menu:
#         print(key)
#     choice = input("1>>:").strip()               #.strip()去掉空
#     if  choice  in  menu :
#         while a and back:                            #讓程式停留在這裡
#             for key2 in menu[choice]:
#                 print(key2)
#             choice2 = input("2>>:")
#             if  choice2 == "b":
#                 a = False
#             if  choice2 == "q":
#                 back = False
#             if  choice2  in  menu[choice]:
#                 while a and back :
#                     for  key3  in menu[choice][choice2]:
#                         print(key3)
#                     choice3 = input("3>>:")
#                     if  choice3 == "b" :
#                         a = False
#                     if  choice3 == "q":
#                         back = False
#                     if  choice3  in menu[choice][choice2]:
#                         while a and back  :
#                             for key4 in menu[choice][choice2][choice3]:
#                                 print(key4)
#                             choice4 = input (">>4:").strip()
#                             if  choice4 == "b" :
#                                 a = False
#                             if choice4 == "q":
#                                 back = False
#                                 break
#                         else :
#                             a = True
#                 else :
#                     a = True
#         else:
#             a = True
           

進階版

menu = {
    "北京":{
        "海澱":{
            "五道口":{
                "soho":{},
                "網易":{},
                "google":{},
            },
            "上地":{
                "百度":{},
            },
            "中關村":{
                "愛奇藝":{},
                "汽車之家":{},
                "youku":{},
            },
        },
        "昌平":{
            "沙河":{},
            "天通苑":{},
            "回龍觀":{},
        },
        "朝陽":{},
    },
    "上海":{},
    "山東":{},
}
current_layer = menu
#parent_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_layer = current_layer
        parent_layers.append(current_layer)
        current_layer=current_layer[choice] #改成子層
    elif choice == "b":
        #current_layer = parent_layer  # 把子層變成父層
        if  parent_layers: #[]
            current_layer = parent_layers.pop()
    elif choice == "q":
        current_layer = menu
    else:
        print("none")