开始使用免费开始使用

操作字典

到目前为止,您已经把配料及其克数添加进去了。

现在,您想练习用不同的方法访问字典的内容,以确认字典按预期设置,并为后续的配方缩放做准备。

本练习是课程的一部分

Python for Developers 入门

查看课程

练习说明

  • 使用一种方法从 recipe 字典中检索所有键,并将结果赋给 ingredient_names
  • 使用一种方法检索 recipe 字典中的所有值,并将结果赋给 quantities
  • 使用一种方法检索 recipe 字典中的所有键值对,并将结果赋给 recipe_items

交互式实操练习

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

# Get all ingredient names
ingredient_names = ____

# Get all quantities
quantities = ____

# Get all key-value pairs
recipe_items = ____

print("Ingredient names:", ingredient_names)
print("Quantities:", quantities)
print("Recipe items:", recipe_items)
编辑并运行代码