開始使用免費開始

單行 docstring

Docstring 用來說明函式的目的。雖然函式名稱應該具備描述性,但也要兼顧名稱長度,因此可以透過 docstring 提供更多細節。

在本練習中,你要為先前建立的 calculate_discount() 函式加入一個單行 docstring。

本練習屬於課程

Intermediate Python for Developers

檢視課程

練習說明

  • 新增一段 docstring:"""Calculate the discounted price of a product."""
  • 使用適當的屬性來存取該函式的 docstring。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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.____)
編輯並執行程式碼