开始使用免费开始使用

使用字典的动机

为了理解为什么字典有用,请查看脚本中定义的两个列表。countries 包含一些欧洲国家的名称,capitals 列出了对应的首都名称。

本练习是课程的一部分

Python 中级

查看课程

练习说明

  • countries 上使用 index() 方法,找到 'germany' 的索引。将其存为 ind_ger
  • 使用 ind_gercapitals 列表中访问德国的首都,并打印出来。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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
编辑并运行代码