python实现三级菜单间的前后跳转

Posted likepython

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实现三级菜单间的前后跳转相关的知识,希望对你有一定的参考价值。

#录入菜单
def createMenu():
    Menu = {}
    provinces = ["陕西","四川","江苏"]

    shanXi_cities = ["西安","宝鸡"]
    siChuan_cities = ["成都","绵阳"]
    jiangSu_cities = ["南通","苏州"]
    
    xiAn_locals = ["高新区","郭杜镇"]
    baoJi_locals = ["鸡1区","鸡2区"]
    chengDu_locals = ["锦江区","武侯区"]
    mianyang_locals = ["羊1区","羊2区"]
    nanTong_locals = ["通1区","通2区"]
    suZhou_locals = ["苏1区","苏2区"]

    for i,j in zip([[shanXi_cities,[xiAn_locals,baoJi_locals]],[siChuan_cities,[chengDu_locals,mianyang_locals]],[jiangSu_cities,[nanTong_locals,suZhou_locals]]],provinces):
        menu = {}
        for m,n in zip(i[0],i[1]):
            menu[m] = n
        Menu[j] = menu
    return Menu

#打印一级菜单
def printProvince(Menu):
    print("The provinces are listed as below ----------------------")
    for i in Menu.items():
        print(i[0])
    print("----------------------")
#打印二级菜单
def printCities(Menu,province):
    print("The cities are listed as below ----------------------")
    for i in Menu[province].items():
        print(i[0])
    print("----------------------")
    return province
#打印三级菜单
def printLocals(Menu,province,city):
    print("The district are listed as below ----------------------")
    for i in Menu[province][city]:
        print(i)
    print("----------------------")

if __name__==‘__main__‘:
    Menu = createMenu()
    printProvince(Menu)
    userInput = []    #用来判别处于哪一级菜单

    while(len(userInput)>=0):
        #位于一级菜单
        if(len(userInput) == 0):
            province = input("Enter the province you wanted: ")
            #回退仍处于一级菜单
            if province == "b":
                printProvince(Menu)
            #退出
            elif province == "exit":
                break
            #进入二级菜单
            else:
                while True:
                    try:
                       printCities(Menu,province)
                       userInput.append(province)
                       break
                    except Exception as e:
                       province = input("省名输入错误,请重新输入:")
                            
        #位于二级菜单
        elif (len(userInput) == 1):
            city = input("Enter the city you wanted: ")
            #回退到一级菜单
            if city == "b":
                userInput.pop()
                printProvince(Menu)
            #退出
            elif city == "exit":
                break
            #进入三级菜单
            else:
                while True:
                    try:
                        printLocals(Menu,province,city)
                        userInput.append(city)
                        break
                    except Exception as e:
                        city = input("城市名输入错误,请重新输入:")
        #位于三级菜单
        elif (len(userInput) == 2):
            choose = input("enter b to renturn to the previous menu: ")
            #回退到二级菜单
            if choose == "b":
                userInput.pop()
                printCities(Menu,userInput[0])

       #退出
            else:
                break

 
    
    
    









































































































以上是关于python实现三级菜单间的前后跳转的主要内容,如果未能解决你的问题,请参考以下文章

(转)Python字典实现三级菜单

python实现三级菜单

python 字典实现三级菜单

三级菜单的实现(python程序)

python--------三级菜单实现

python三级菜单的实现