操作字典
到目前为止,您已经把配料及其克数添加进去了。
现在,您想练习用不同的方法访问字典的内容,以确认字典按预期设置,并为后续的配方缩放做准备。
本练习是课程的一部分
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)