遍历字典

# 6-5
rivers_countrys = {'nile': 'egypt', 'yellow river': 'china', 'the ganges river': 'india'}

for river, country in rivers_countrys.items():
    print(river.title() + " flows through " + country.title())

for river in rivers_countrys.keys():
    print(river.title())

for country in rivers_countrys.values():
    print(country.title())

# 6-6
favorite_languages = {
    'lilongbin': 'java',
    'zhaohaibing': 'c',
    'jian': 'python',
}
peoples = ['lilongbin', 'zhaohaibing', 'jian', 'taohuaxian', 'zhudi']

for people in peoples:
    if people in favorite_languages.keys():
        print("Hi " + people.title() + ".Thank you for participating in the survey!")
    else:
        print("Hi " + people.title() + ".Can you accept our investigation?")
Nile flows through Egypt
Yellow River flows through China
The Ganges River flows through India
Nile
Yellow River
The Ganges River
Egypt
China
India
Hi Lilongbin.Thank you for participating in the survey!
Hi Zhaohaibing.Thank you for participating in the survey!
Hi Jian.Thank you for participating in the survey!
Hi Taohuaxian.Can you accept our investigation?
Hi Zhudi.Can you accept our investigation?
    原文作者:庵下桃花仙
    原文地址: https://www.jianshu.com/p/7206ae5a8d9c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞