今天就來點應用的,透過之前所學的,加上前一篇的字典後,我們就來實作三層式地址查詢
的功能
目前需求是:
大家可以先想想,怎麼做呢?等等後續再更新上來
來來更新嚕,先上一版未優化的,雖然看起來很醜,但...基本上功能都有做到了
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
data = {
"台北市":{
"中正區":{
"水源里":["羅斯福路4段"],
"富水里":["永春街"],
"文盛里":["羅斯福路3段"],
},
"大同區":{
"國順里":["延平北路3段"],
"建明里":["重慶北路一段"],
"光能里":["民生西路"],
},
"信義區":{
"西村里":["基隆路1段"],
"正和里":["仁愛路4段"],
"興隆里":["基隆路1段"],
},
},
"新北市":{
"板橋區":{
"黃石里":["宮口街"],
"自強里":["中正路"],
"金華里":["金華街"],
},
"中和區":{
"中原里": ["永和路"],
"連和里": ["連城路"],
"連城里": ["連城路"],
},
"新莊區":{
"中平里": ["中平路"],
"中原里": ["中港路"],
"昌平里": ["中華路2段"],
},
},
"台中市":{
"北屯區":{
"北屯里": ["太原路"],
"北京里": ["國校巷"],
"北興里": ["北屯路"],
},
"后里區": {
"廣福里": ["寺山路"],
"仁里里": ["圳寮路"],
"義里里": ["梅里路"],
},
"北區": {
"頂厝里": ["永興街"],
"賴村里": ["梅亭街"],
"賴旺里": ["中清路一段"],
}
}
}
exit_flag = False
while not exit_flag:
for city in data:
print(city)
choice_city = input("請選擇你要的城市:")
if choice_city in data:
while not exit_flag:
for district in data[choice_city]:
print("\t", district)
choice_district = input("請選擇你要的行政區:")
if choice_district in data[choice_city]:
while not exit_flag:
for li in data[choice_city][choice_district]:
print("\t\t", li)
choice_li = input("請選擇你要的里名:")
if choice_li in data[choice_city][choice_district]:
for road in data[choice_city][choice_district][choice_li]:
print("\t\t", road)
choice_back = input("已到最底層了,按b返回上一層:")
if choice_back == "b":
pass
elif choice_back == "q":
exit_flag = True
if choice_li == "b":
break
elif choice_li == "q":
exit_flag = True
if choice_district == "b":
break
elif choice_district == "q":
exit_flag = True
if choice_city == "q":
exit_flag = True
我不知道『里』的英文是啥,只好用『li』代替了 @@"