16-5 涵蓋所有國家 :本節(jié)制作人口地圖時,對于大約12個國家,程序不能自動確定其兩個字母的國別碼。請找出這些國家,在字典COUNTRIES 中找到它們的國別
碼;然后,對于每個這樣的國家,都在get_country_code() 中添加一個if-elif 代碼塊,以返回其國別碼:
if country_name == 'Yemen, Rep.'
return 'ye'
elif
--snip--
將這些代碼放在遍歷COUNTRIES 的循環(huán)和語句return None 之間。完成這樣的修改后,你看到的地圖將更完整。
解答方式:
應該把
country_code.py
from pygal_maps_world.i18n import COUNTRIES
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name == country_name:
return code
return None
print(get_country_code('Andorra'))
print(get_country_code('United Arab Emirates'))
print(get_country_code('Afghanistan'))
改為
from pygal_maps_world.i18n import COUNTRIES
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name == 'Yemen':#Yemen, Rep.無法使用,修改為Yemen
return 'ye'
elif name == country_name:
return code
return None
print(get_country_code('Andorra'))
print(get_country_code('United Arab Emirates'))
print(get_country_code('Afghanistan'))
```