处理多个默认参数
接下来,我们将使用一个带有多个默认参数的函数来解开第二个封印。我们将创建一个函数,用于描述向神灵献上的祭品。
仍在 ~/project
目录中,在我们的 incantation.py
文件中添加一个名为 make_offering
的新函数,该函数将接受三个参数:food
(默认值为 "grain")、incense
(默认值为 "frankincense")和 gemstone
(默认值为 "lapis lazuli")。
incantation.py
中的示例代码:
def make_offering(food="grain", incense="frankincense", gemstone="lapis lazuli"):
return f"Offering {food}, {incense}, and {gemstone} to please the gods."
## Test the function with default and custom arguments
print(make_offering())
print(make_offering(food="dates", gemstone="turquoise"))
运行更新后的文件:
python3 incantation.py
预期输出:
Offering grain, frankincense, and lapis lazuli to please the gods.
Offering dates, frankincense, and turquoise to please the gods.