下面的代碼在return Total
行處拋出錯誤無效語法。我認爲我的while循環有一個問題,但我沒有看到它。我試圖用下面的代碼來計算階乘。因此,print factorial(4)
部分將計算4 * 3 * 2 * 1並將24
返回到控制檯。語法無效,函數將不接受返回,而是拋出無效語法
def factorial(x):
Total = int(x)
fact = int(x) - 1
while fact >= 1:
Total *= fact
fact -= 1\
return Total
print factorial(4)
我也嘗試下面的代碼,以便它會print Total
VS Return Total
。
def factorial(x):
Total = int(x)
fact = int(x) - 1
while fact >= 1:
Total *= fact
fact -= 1\
print Total
print factorial(4)
當我使用1作爲輸入返回:1
你的功能墜毀輸入,因爲你的功能*拋出一個「不支持的操作數類型(S):‘NoneType’和‘INT’ 「錯誤。
fact -= 1\
的\
表示續行,這將拋出一個語法錯誤
一個很好的規律可循:
那你爲什麼還要在擺脫了'''\'''字符的結束你的問題? – wnnmaw
請發佈更多位於您所展示內容上方的代碼。 – wheaties