爲什麼蟒蛇接受驗證碼:爲什麼python要求文檔字符串縮進?
def is_right_triangle(leg1,leg2,hypotenuse):
"""function for checking whether or not set of points makes a right triangle"""
return leg1 ** 2 + leg2 ** 2 == hypotenuse ** 2
但在這個代碼,其中的文檔字符串沒有縮進
def is_right_triangle(leg1,leg2,hypotenuse):
"""function for checking whether or not set of points makes a right triangle"""
return leg1 ** 2 + leg2 ** 2 == hypotenuse ** 2
它拋出一個「預期的縮進塊」的錯誤。
爲什麼python會關心文檔字符串是否縮進?
因爲docstring是一個實際的字符串,縮進在Python中很重要。 –
換句話說,python認爲文檔字符串是函數的一部分。 – Tookie345
這只是一個字符串。當然,它與函數關聯,但重要的是它需要根據Python的語法規則進行分析。 –