1. Learn
  2. /
  3. 课程
  4. /
  5. Python 中级

Connected

道练习

遍历字典

在 Python 3 中,您需要使用 items() 方法来遍历字典:

world = { "afghanistan":30.55, 
          "albania":2.77,
          "algeria":39.21 }

for key, value in world.items() :
    print(key + " -- " + str(value))

还记得 europe 字典吗?它以一些欧洲国家名为键,以其首都为对应的值。请编写一个循环来遍历它吧!

说明

100 XP

编写一个 for 循环,遍历 europe 的每个 key:value 对。在每次迭代中,打印 "the capital of x is y",其中 x 是键,y 是对应的值。