zoukankan      html  css  js  c++  java
  • 三级菜单_使用os模块

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-  
    # by wk
    
    import os
    
    def list_cities(city_list):
        while True:
            t = []
            for k, v in enumerate(city_list):
                print(k, v['city'])
                t.append(str(k))       #判断列表有多少值
            # print(t)
            city_id = input('请选择城市:')
            if city_id in t:
                return city_id
            else:
                print('请重新输入')
    
    def list_district(city_id):
        while True:
            t = []
            for k, v in enumerate(city_list[city_id]['district']):
                print(k, v['dist'])
                t.append(str(k))         #判断列表有多少值
            district_id = input('请选择区域(b返回上一层):')
            if district_id in t:
                return district_id
            elif district_id == 'b':
                return 'back'
            else:
                print('请重新输入')
    
    def list_area(city_id,district_id):
        while True:
            t = []
            for k, v in enumerate(city_list[city_id]['district'][district_id]['area']):
                print(k, v)
                t.append(str(k))        #判断列表有多少值
            area_id = input('请选择地点(b返回上一层):')
            if area_id in t:
                return area_id
            elif area_id == 'b':
                return 'back'
            else:
                print('请重新输入')
    
    def my_choose(city_id,district_id,area_id):
        return city_list[city_id]['district'][district_id]['area'][area_id]
    
    if __name__ == "__main__":
        city_list = [
            {'city': '北京', 'district':
                [{'dist':'海淀','area':['海1', '海2', '海3']},
                 {'dist': '东城', 'area': ['东1', '东2', '东3']},
                 {'dist':'西城','area':['西1', '西2', '西3']},
                 {'dist': '朝阳', 'area': ['朝1', '朝2', '朝3']}]
             },
            {'city': '上海', 'district':
                [{'dist': '黄浦', 'area': ['黄1', '黄2', '黄3']},
                 {'dist': '徐汇', 'area': ['徐1', '徐2', '徐3']},
                 {'dist': '长宁', 'area': ['长1', '长2', '长3']},
                 {'dist': '天河', 'area': ['天1', '天2', '天3']}]
             },
            {'city': '广州', 'district':
                [{'dist': '荔湾', 'area': ['荔1', '荔2', '荔3']},
                 {'dist': '越秀', 'area': ['越1', '越2', '越3']},
                 {'dist': '海珠', 'area': ['海1', '海2', '海3']},
                 {'dist': '天河', 'area': ['天1', '天2', '天3']}]
             },
        ]              #创建一张城市列表
        while True:
            city_id = list_cities(city_list)      #列出城市
            while True:
                district_id = list_district(int(city_id))       #列出选中城市下的区
                if district_id == 'back':
                    break               #返回上一层循环
                else:
                    while True:
                        area_id = list_area(int(city_id),int(district_id))
                        if area_id == 'back':
                            break       #返回上一层循环
                        else:
                            end_choose = my_choose(int(city_id), int(district_id), int(area_id))
                            print('最终选择:',end_choose)
                            os._exit(1)
  • 相关阅读:
    WPF 实现窗体拖动
    CAP带你轻松玩转ASP.NETCore消息队列
    ASP.NET Core开发-获取所有注入(DI)服务
    k8s实战为aspnetcore.webapi微服务注入配置信息
    详解docker实战之搭建私有镜像仓库
    自己动手破解Z.EntityFramework.Extensions 4.0.11.0的方法
    可以获取随机图片的API收集 必应 等
    一键发布部署vs插件[AntDeploy],让net开发者更幸福
    比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module
    编译nginx的源码安装subs_filter模块
  • 原文地址:https://www.cnblogs.com/godspeed034/p/7245586.html
Copyright © 2011-2022 走看看