multi-level menu

# @ author: THIRTEEN
#@date:2018.4.22 17:36

menu={
    "Beijing":{
        "Chaoyang":{
            "International Trade":{
                "CICC":{},
                "CCTV":{},
            },
            "Wangjing":{
                "Momo":{},
                "360":{},
            },
            "Sanlitun":{
                "Uniqlo":{},
                "apple":{},
            },
        },
        "Changping":{
            "Shahe":{
            },
            "Tiantongyuan":{
                "Chain Home":{},
            },
            "Huilongguan":{
                
            },
        },
        "Haidian":{
            "Wudaokou":{
                "Google":{},
                "Sogo":{},
            },
            "Zhongguancun":{
                "QQ":{},
                },
        },
    },
    "Shanghai":{
        "Pudong":{
                "Lujiazui":{
                    "Goldman Sachs":{},
                },
        },
    },
    "Sichuan":{
        "Dazhou":{
        
        },
        "Chengdu":{
        
        },
    },
}

current_layer=menu #Current layer
parent_list=[]
exit_falg=False

while not exit_falg:
    if len(current_layer)!=0:
        for key in current_layer:
            print(key)
        print()
        choice=input(">>>:").strip()
        if len(choice)==0:continue
        if choice in current_layer:
            parent_list.append(current_layer)#Append a current_layer dictionary at the end of the list
            current_layer=current_layer[choice]
        elif choice =="b":
            if parent_list:
                current_layer=parent_list.pop()#The fallback is to delete and return the last value of parent_list
            else:
                print("It has reached the first layer".center(50,"-"))
        elif choice =="q":
            exit_falg=True
        else:
            print("No item!".center(50,"-"))
    else :
        print("In the end! [Return to the previous level: b]; [Exit: q] ".center(50,"-"))
        current_layer=parent_list.pop()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324732977&siteId=291194637