[英]How to correctly define a function?
在 python 中,我嘗試執行以下操作來定義函數:
count_letters(word) = count_vowels(word) + count_consonants(word)
但由於某種原因,這是錯誤的。 我收到此錯誤:
SyntaxError: can't assign to function call
我該如何解決?
謝謝
函數調用count_letters(word)
是不可賦值的。 就這么簡單。
我不相信它可以在 python 中工作,你應該有這樣的錯誤:
SyntaxError: can't assign to function call
這不是您在 python 中聲明函數的方式。 你想寫的是:
def count_letters(word):
return count_vowels(word) + count_consonants(word)
也就是說,如果您已經有一個count_vowels
和一個count_consonants
函數。
您需要用適當的函數定義替換它:
def count_letters(word):
return count_vowels(word) + count_consonants(word)
您嘗試使用的語法不是有效的 Python。
可能是你想做的是
def count_letters(word):
return count_vowels(word) + count_consonants(word)
如果我收集正確,您正在嘗試創建一個函數。 但是,您現在擁有的語法無效-對於 Python,您似乎正在嘗試為函數調用 ( count_letters(word)
) count_letters(word)
,這在 Python 中是不允許的。 count_letters = count_vowels(word) + count_consonants(word)
會起作用,但不是你想要的。
為了聲明該函數,您應該執行以下操作:
def count_letters(word):
return count_vowels(word) + count_consonants(word)
具有定義的函數:count_vowels(word) 和 count_consonants(word)
那么你可以這樣做:count_letters = count_vowels(word) +count_consonants(word)
希望有幫助! 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.