码迷,mamicode.com
首页 > 其他好文 > 详细

省县市三级联动

时间:2017-07-14 16:26:14      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:python脚本

需求
    省县市三级联动
    用户输入河北,则打印河北省下的市,用户输入市,则显示该河北省的这个市下的县
测试信息
dic = {
    "河北":{
        "石家庄":["鹿泉","藁城","元氏"],
        "邯郸":["永年","涉县","磁县"]
    },
    "河南":{
        "周口":["太康","商水","淮阳"],
        "郑州":["金水","二七","管城"]
    },
    "山西":{
        "太原":["迎泽","清徐","阳曲"],
        "大同":["阳高","天镇","灵丘"]
    }
}

技术分享

dic = {
    "河北":{
        "石家庄":["鹿泉","藁城","元氏"],
        "邯郸":["永年","涉县","磁县"]
    },
    "河南":{
        "周口":["太康","商水","淮阳"],
        "郑州":["金水","二七","管城"]
    },
    "山西":{
        "太原":["迎泽","清徐","阳曲"],
        "大同":["阳高","天镇","灵丘"]
    }
}
while True:
    print(‘请在以下省份中选择:‘)
    for i in dic.keys():
        print(i)
    pro=input(‘输入你要查询的省份(退出请按q):‘).strip().lower()
    if pro == ‘q‘:
        print(‘再见!‘)
        exit()
    if pro not in dic.keys():
        print(‘要查询的省份不存在或者你的输入有误,请重新输入‘)
        continue
    else:
        tag=True
        while tag:
            print(‘%s有以下城市:‘ % pro)
            for c in dic[pro].keys():
                print(c)
            city=input(‘请输入你要查询的城市(回到省份查找请按p,退出请按q):‘).strip().lower()
            if city == ‘q‘:
                print(‘再见!‘)
                exit()
            if city == ‘p‘:
                break
            if city not in dic[pro].keys():
                print(‘要查询的城市不存在或者你的输入有误,请重新输入‘)
                continue
            else:
                while tag:
                    print(‘%s有以下县区‘ % city)
                    for cou in dic[pro][city]:
                        print(cou)
                    county=input(‘重新选择省份请按p,重新选择城市请按c,退出请按q:‘).strip().lower()
                    if county == ‘q‘:
                        print(‘再见!‘)
                        exit()
                    if county == ‘c‘:
                        break
                    if county == ‘p‘:
                        tag=False
                        break
                    else:
                        print(‘输入错误,请重新输入‘)
                        continue


本文出自 “lyndon” 博客,请务必保留此出处http://lyndon.blog.51cto.com/11474010/1947436

省县市三级联动

标签:python脚本

原文地址:http://lyndon.blog.51cto.com/11474010/1947436

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!