码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-12-27 18:31:51      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:[1]   回退   str   span   round   ret   color   ems   main   

#录入菜单
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\n----------------------")
    for i in Menu.items():
        print(i[0])
    print("----------------------")
#打印二级菜单
def printCities(Menu,province):
    print("The cities are listed as below\n----------------------")
    for i in Menu[province].items():
        print(i[0])
    print("----------------------")
    return province
#打印三级菜单
def printLocals(Menu,province,city):
    print("The district are listed as below\n----------------------")
    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实现三级菜单间的前后跳转

标签:[1]   回退   str   span   round   ret   color   ems   main   

原文地址:https://www.cnblogs.com/likePython/p/10185192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!