使用字典的动机
为了理解为什么字典有用,请查看脚本中定义的两个列表。countries 包含一些欧洲国家的名称,capitals 列出了对应的首都名称。
本练习是课程的一部分
Python 中级
练习说明
- 在
countries上使用index()方法,找到'germany'的索引。将其存为ind_ger。 - 使用
ind_ger从capitals列表中访问德国的首都,并打印出来。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']
# Get index of 'germany': ind_ger
# Use ind_ger to print out capital of Germany