我對python完全陌生,而且在返回factorial變量時遇到了問題。我想創建一個遞歸函數。我在第12,9和7行不斷收到「分配前引用的局部變量」階乘「的錯誤。任何幫助表示讚賞。Python:返回一個變量
global userinput
userinput = input('What integer would you like to to find a total factorial for?')
def recursion(counter):
if counter < 3:
factorial = 1
if counter <= userinput:
factorial = factorial * counter
increase = counter + 2
recursion(increase)
else:
return factorial
efactorial = recursion(2)
ofactorial = recursion(1)
tfactorial = efactorial + ofactorial
這部分:'factorial = factorial * counter'參考'factorial'(在右邊),但它尚未設置爲數字。 – James
順便說一句,數學模塊具有階乘函數 – abccd