zoukankan      html  css  js  c++  java
  • python 三级菜单

    # 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
    dic = {
        'k1': {
            'k11': {
                'k111': 'v111',
                'k112': 'v112'
            },
            'k12': {
                'k121': 'v121',
                'k122': 'v122'
            }
        },
        'k2': {
            'k21': {},
            'k22': {}
        }
    }
    # 利用对战实现
    li = [dic]
    while li:
        for i in li[-1]:
            print(i)
        k = input("Please input key:")
        if k in li[-1].keys() and li[-1][k]:
            li.append(li[-1][k])
        elif k == 'b':
            li.pop()
        elif k == 'q':
            break
        else:
            continue
  • 相关阅读:
    getline在windows系统下的怪现象
    android camera根据屏幕图像大小设置显示
    android camera以时间来命名照片文件
    android 基本的画图方法
    android 开发使用剪切板传递数据
    用keytool生成一个自签名证书,并导出到文件
    VBA,两个sheet根据第一列id相同比较其余列,不同的值放到sheet3中
    摄像头视频采集压缩及传输
    Extjs 3根据数据库动态生成checkboxgroup(extjs系列1)
    Extjs 3,jstl根据数据库数据自动构建FormPanel(Extjs系列2)
  • 原文地址:https://www.cnblogs.com/wt7018/p/10809326.html
Copyright © 2011-2022 走看看