基于python实现三级菜单

意义

通过联系可以熟练掌握,python字典的创建、修改、遍历 ,其中遍历我用的是 for  i,j in enumerate()的方法。

业务流程     

输入选择选择进入的城市 

1.四川

2 贵州

 q 退出

一、选择 1、2进入省份,选择Q退出 ,必须能滚键盘

二、选择1、 2进入城市,选择Q返回上一级菜单,同样必须滚键盘

1 成都
2 绵阳

q 返回上一级菜单

三、查看所选择的城市,选择Q返回上一级菜单,同样必须滚键盘

实现

#___author  Laughing
#date
Province={'四川':{},'贵州':{}}                        #初始化省、城、县数据的字典
Sichuan_city={'成都':{},'绵阳':{}}                    #红线不用理 ,复制错误
Guizhou_city={'遵义':{},'贵阳':{}}
Chengdu_county={'青羊区':{},'锦江区':{}}
Mianyang_county={'绵阳城区':{},'绵阳郊区':{}}
Zhunyi_county={'遵义市区':{},'遵义郊区':{}}
Guiyang_county={'贵阳市区':{},'贵阳郊区':{}}
# Province['四川']=Sichuan_city
# Province['贵州']=Guizhou_city
# Sichuan_city['成都']=Chengdu_county
# Sichuan_city['绵阳']=Mianyang_county
# Guizhou_city['遵义']=Zhunyi_county
# Guizhou_city['贵阳']=Guiyang_county
# for i,j in enumerate(Province,1):
#     print(i,j)
while True:
    for i, j in enumerate(Province, 1):              #省级菜单打印
        print(i, j)
    print('q 退出')
    choice=input()
    if choice.isdigit():                            #是否为整数判断
        choice = int(choice)
        if choice != 1 and choice != 2 :             #越界判断
            print('please input 1 or 2')
            for i, j in enumerate(Province, 1):
                print(i, j, )
            continue
    elif choice=='q'or choice == 'Q':
        print('bye  ')
        break
    else:
        print("plases input digit")
        for i, j in enumerate(Province, 1):
            print(i, j, )
        continue
    if choice == 1:                                      #省选择  后面大部分代码相同
        while True:
            Province['四川'] = Sichuan_city
            for i, j in enumerate(Sichuan_city, 1):
                print(i, j)
            print("q 返回上一级菜单")
            choice = input()
            if choice.isdigit():  # 是否为整数判断
                choice = int(choice)
                if choice != 1 and choice != 2:  # 越界判断
                    print('please input 1 or 2')
                    for i, j in enumerate(Sichuan_city, 1):
                        print(i, j, )
                    continue
            elif choice == 'q' or choice == 'Q':
                print('成功返回上级菜单')
                break
            else:
                print("plases input digit")
                for i, j in enumerate(Sichuan_city, 1):
                    print(i, j, )
                continue
            if choice == 1:                                     #城市选择  后面大部分代码相同
                while True:
                    Sichuan_city['成都'] = Chengdu_county
                    for i, j in enumerate(Chengdu_county, 1):
                        print(i, j)
                    print("q 返回上一级菜单")
                    choice = input()
                    if choice.isdigit():  # 是否为整数判断
                        choice = int(choice)
                        if choice != 1 and choice != 2:  # 越界判断
                            print('please input 1 or 2')
                            for i, j in enumerate(Chengdu_county, 1):
                                print(i, j, )
                            continue
                    elif choice == 'q' or choice == 'Q':
                        print('成功返回上级菜单')
                        break
                    else:
                        print("plases input digit")
                        for i, j in enumerate(Chengdu_county, 1):
                            print(i, j, )
                        continue
            if choice == 2:                                    #城市选择  后面大部分代码相同
                while True:
                    Sichuan_city['绵阳'] = Mianyang_county
                    for i, j in enumerate(Mianyang_county, 1):
                        print(i, j)
                    print("q 返回上一级菜单")
                    choice = input()
                    if choice.isdigit():  # 是否为整数判断
                        choice = int(choice)
                        if choice != 1 and choice != 2:  # 越界判断
                            print('please input 1 or 2')
                            for i, j in enumerate(Mianyang_county, 1):
                                print(i, j, )
                            continue
                    elif choice == 'q' or choice == 'Q':
                        print('成功返回上级菜单')
                        break
                    else:
                        print("plases input digit")
                        for i, j in enumerate(Mianyang_county, 1):
                            print(i, j, )
                        continue

    if choice == 2:                                        #省选择  后面大部分代码相同
        while True:
            Province['贵州'] = Guizhou_city
            for i, j in enumerate(Guizhou_city, 1):
                print(i, j)
            print("q 返回上一级菜单")
            choice = input()
            if choice.isdigit():  # 是否为整数判断
                choice = int(choice)
                if choice != 1 and choice != 2:  # 越界判断
                    print('please input 1 or 2')
                    for i, j in enumerate(Guizhou_city, 1):
                        print(i, j, )
                    continue
            elif choice == 'q' or choice == 'Q':
                print('成功返回上级菜单')
                break
            else:
                print("plases input digit")
                for i, j in enumerate(Guizhou_city, 1):
                    print(i, j, )
                continue
            if choice == 1:                                         #城市选择  后面大部分代码相同
                while True:
                    Guizhou_city['遵义'] = Zhunyi_county
                    for i, j in enumerate(Zhunyi_county, 1):
                        print(i, j)
                    print("q 返回上一级菜单")
                    choice = input()
                    if choice.isdigit():  # 是否为整数判断
                        choice = int(choice)
                        if choice != 1 and choice != 2:  # 越界判断
                            print('please input 1 or 2')
                            for i, j in enumerate(Zhunyi_county, 1):
                                print(i, j, )
                            continue
                    elif choice == 'q' or choice == 'Q':
                        print('成功返回上级菜单')
                        break
                    else:
                        print("plases input digit")
                        for i, j in enumerate(Zhunyi_county, 1):
                            print(i, j, )
                        continue
            if choice == 2:                                        #城市选择  后面大部分代码相同
                while True:
                    Guizhou_city['贵阳'] = Guiyang_county
                    for i, j in enumerate(Guiyang_county, 1):
                        print(i, j)
                    print("q 返回上一级菜单")
                    choice = input()
                    if choice.isdigit():  # 是否为整数判断
                        choice = int(choice)
                        if choice != 1 and choice != 2:  # 越界判断
                            print('please input 1 or 2')
                            for i, j in enumerate(Guiyang_county, 1):
                                print(i, j, )
                            continue
                    elif choice == 'q' or choice == 'Q':
                        print('成功返回上级菜单')
                        break
                    else:
                        print("plases input digit")
                        for i, j in enumerate(Guiyang_county, 1):
                            print(i, j, )
                        continue

优化(重点)  

以上代码,重复量太高,为了提高开发可行性。我们进行如下优化:(优化部分吃完饭再写 ,现在先吃饭~~)

吃饭完了 现在继续  1. 首先多字典接收数据方面在大数据量的情况下改正,但针对此题 明显选择 单字典好

2.上面的代码用enumerate方法加了序号,就导致代码重复率很好,但不用enumerate方法 ,只用  for i in msg就可大大减少重复率,因为题目中没有提到要加序号 所以我们可以看出审题很重要。

msg={
    '四川':{
        '成都':['锦江区','青羊区','郫都区'],
        '绵阳':['绵阳城区','绵阳郊区']
    },
    '贵州':{
        '遵义':['遵义市区','遵义郊区'],
        '贵阳':['贵阳市区','贵阳郊区']
    }
}
while True:
    for i in  msg:                  # province
        print(i)
    choice = input('>>-:').strip()
    if choice in msg:
            while True:
                for i in  msg[choice]:        # city
                    print(i)
                print("input q to Return to previous menu")
                choice1 = input('>>-:').strip()
                if choice1 in msg[choice]:
                    while True:
                        for i in msg[choice][choice1]:
                            print(i)
                        print("input q to Return to previous menu")
                        choice2 = input('>>-:').strip()
                        if choice2 == 'q' or choice2 == 'Q':
                            break
                elif choice1 == 'q' or choice1 == 'Q':
                    break
                else:
                    print("plases input right string")
                    continue
    else:
        print("plases input right string")

再次优化(超级重点)  

凡是重复都可优化,1.循环优化用时间段(下图:before.flag=当前位置,now.flag=现在位置)那么我们在处理上就可以用位置变量代替循环。2.名词循环了用接收变量优化(上图:choice)

msg={
    '四川':{
        '成都':['锦江区','青羊区','郫都区'],
        '绵阳':['绵阳城区','绵阳郊区']
    },
    '贵州':{
        '遵义':['遵义市区','遵义郊区'],
        '贵阳':['贵阳市区','贵阳郊区']
    }
}
now_flag=msg                  #现在位置
before_flag=[]                #上一个位置
while True:
    for i in  now_flag:                  # province
        print(i)
    choice = input('>>-:').strip()           #相当于在输入时的值决定当前位置
    if choice in now_flag:
        before_flag.append(now_flag)       #上一个位置等于上一个位置
        now_flag=now_flag[choice]          #现在的位置等于现在的位置
    elif choice == 'q':
            if now_flag:             #如果是空 返回FALSE  不执行
                now_flag = before_flag.pop()       #pop的返回值是删除的那个值 比如a[1,2,3] b=pop(a)=3
    else:print('wrong')


发布了20 篇原创文章 · 获赞 0 · 访问量 3811

猜你喜欢

转载自blog.csdn.net/Laughing_G/article/details/80655629