需求 省县市三级联动 用户输入河北,则打印河北省下的市,用户输入市,则显示该河北省的这个市下的县
测试信息 dic = { "河北":{ "石家庄":["鹿泉","藁城","元氏"], "邯郸":["永年","涉县","磁县"] }, "河南":{ "周口":["太康","商水","淮阳"], "郑州":["金水","二七","管城"] }, "山西":{ "太原":["迎泽","清徐","阳曲"], "大同":["阳高","天镇","灵丘"] } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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博客51CTO博客,原文链接http://blog.51cto.com/lyndon/1947436如需转载请自行联系原作者 迟到的栋子
|