编程语言
首页 > 编程语言> > python3三级菜单

python3三级菜单

作者:互联网

# Multilevel menu
menu = {
    '省1': {
        '城市11': {
            '区域111': {},
            '区域112': {},
            '区域113': {},
            '区域114': {}
                 },
        '城市12': {
            '区211': {},
            '区212': {},
            '区213': {}
        },
        '城市13': {
            '区311': {},
            '区312': {},
            '区313': {}
        },
           },
    '省2': {
        '市21': {
            '区211': {},
            '区212': {},
            '区213': {}
        },
        '市22': {
            '区221': {},
            '区222': {},
            '区223': {}
        },
        '市23': {
            '区231': {},
            '区232': {},
            '区233': {}
        }
    },
    '省3': {
        '市31': {
            '区311': {},
            '区312': {},
            '区313': {}
        },
        '市32': {
            '区321': {},
            '区322': {},
            '区323': {}
        },
        '市33': {
            '区331': {},
            '区332': {},
            '区333': {}
        }
    }
        }
exit_flag = False
while not exit_flag:
    print("---------省----------")
    for i in menu:
        print(i)
    choice1 = input("请输入省:")
    if choice1 in menu:
        while not exit_flag:
            print("----------城市---------")
            for i in menu[choice1]:
                print(i)
            choice2 = input("请输入城市:")
            if choice2 in menu[choice1]:
                while not exit_flag:
                    print("-----------区域----------")
                    for i in menu[choice1][choice2]:
                        print(i)
                    choice3 = input("请选择区域:")
                    if choice3 in menu[choice1][choice2]:
                        print("到达底部")
                    elif choice3 == 'b':
                        break
                    elif choice3 == 'q':
                        exit_flag = True
                    else:
                        pass
            elif choice2 == 'b':
                break
            elif choice2 == 'q':
                exit_flag = True
            else:
                pass
    elif choice1 == 'q':
        exit_flag = True
# 优化
city_catalog = {
    '省1': {
        '市11': {
            '区111': {},
            '区112': {},
            '区113': {}
        },
        '市12': {
            '区121': {},
            '区122': {},
            '区123': {}
        },
        '市13': {
            '区131': {},
            '区132': {},
            '区133': {}
        }
    },
    '省2': {
        '市21': {
            '区211': {},
            '区212': {},
            '区213': {}
        },
        '市22': {
            '区221': {},
            '区222': {},
            '区223': {}
        },
        '市23': {
            '区231': {},
            '区232': {},
            '区233': {}
        }
    },
    '省3': {
        '市31': {
            '区311': {},
            '区312': {},
            '区313': {}
        },
        '市32': {
            '区321': {},
            '区322': {},
            '区323': {}
        },
        '市33': {
            '区331': {},
            '区332': {},
            '区333': {}
        }
    }
}
# exit_flag = False
# while not exit_flag:
#     print("---------省-----------")
#     for i in city_catalog:
#         print(i)
#     choice = input("Please enter choice:")
#     if choice in city_catalog:
#         while not exit_flag:
#             print("----------市----------")
#             for i in city_catalog[choice]:
#                 print(i)
#             choice2 = input("Please enter choice:")
#             if choice2 in city_catalog[choice]:
#                 while not exit_flag:
#                     print("-----------区----------")
#                     for i in city_catalog[choice][choice2]:
#                         print(i)
#                     choice3 = input("Please enter choice:")
#                     if choice3 in city_catalog[choice][choice2]:
#                         print("------------last-----------")
#                         for i in city_catalog[choice][choice2][choice3]:
#                             print(i)
#                         while not exit_flag:
#                             choice4 = input("Please enter choice:")
#                             if choice4 == 'b':
#                                 break
#                             elif choice4 == 'q':
#                                 exit_flag = True
#                     elif choice3 == 'b':
#                         break
#                     elif choice3 == 'q':
#                         exit_flag = True
#                     else:
#                         pass
#             elif choice2 == 'b':
#                 break
#             elif choice2 == 'q':
#                 exit_flag = True
#             else:
#                 pass
#     elif choice == 'b':
#         break
#     elif choice == 'q':
#         exit_flag = True
#     else:
#         pass
now_choice = city_catalog
history = []
exit_flag = False
while not exit_flag:
    for i in now_choice:
        print(i)
    choice = input("Please enter choice:")
    if choice in now_choice:
        history.append(now_choice)
        now_choice = now_choice[choice]
    elif choice == 'b':
        now_choice = history.pop()
    elif choice == 'q':
        exit_flag = True
    else:
        pass

 

标签:elif,菜单,choice2,choice,flag,exit,python3,print,三级
来源: https://www.cnblogs.com/wt7018/p/10809326.html