1. 程式人生 > 其它 >《python程式設計從入門到實踐》習題答案第6章-字典(6.11城市)

《python程式設計從入門到實踐》習題答案第6章-字典(6.11城市)

技術標籤:python

# 6-11 城市 :建立一個名為cities 的字典,其中將三個城市名用作鍵;對於每座城市,都建立一個字典,並在其中包含該城市所屬的國家、 人口約數以及一個有關該城市的事實。在表示每座城市的字典中,應包含country 、population 和fact 等鍵。將每座城市的名字以及有關它們的資訊都打印出來。

cities = {'beijing': {'country': 'china', 'population': '13', 'fact': 'beautiful'},
          'new york': {'country': 'usa', 'population': '3.3', 'fact': 'best'},
          'Japan': {'country': 'Tokyo', 'population': '2.3', 'fact': 'fast'},
          }
for city, info in cities.items():
    print("\n"+city+"\n介紹:")
    for key,value in info.items():
        print(key + ":" + value)