开始使用免费开始使用

单行文档字符串

文档字符串用于说明函数的用途。函数名应尽量具有描述性,但也要兼顾长度。因此,文档字符串可以让您提供更多细节。

本练习中,您将为之前创建的 calculate_discount() 函数添加一条单行文档字符串。

本练习是课程的一部分

Python 中级:面向开发者

查看课程

练习说明

  • 添加如下文档字符串:"""Calculate the discounted price of a product."""
  • 使用相应的属性访问该函数的文档字符串。

交互式实操练习

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

def calculate_discount(price, discount_percent=15, round_result=True):
    # Add a single-line docstring
    ____
    
    discounted_price = price - (price * (discount_percent / 100))
    if round_result == True:
        return round(discounted_price, 2)
    else:
        return discounted_price

# Access the docstring
print(calculate_discount.____)
编辑并运行代码