三级菜单_使用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)

 

posted @ 2017-07-27 16:32  GodSpeed034  阅读(124)  评论(0编辑  收藏  举报