0
def factorial(n,fact):
    if n!=0:
        fact=fact*n
        factorial(n-1,fact)
    else:
        print(fact)
        return fact
print(factorial(5,1))

I have done factorial program.but when I am printing it gets 120 but when I am returning it saying none When I am using both i am get only 120.it is not getting 2 120 in my output what go wrong could you explain it. Why it returning none is my question why not 120.

I am not get 120 120.it getting only 120 and none I am getting.why I am getting none.could you explain it to me

tkausl
  • 13,686
  • 2
  • 33
  • 50
  • 1
    you need to return `factorial(n-1,fact)` also – Albin Paul Jul 11 '23 at 02:41
  • 1
    If `n` is non-zero, you don't return anything. See the documentation for the `return` statement. If a function reaches the end without executing an explicit return, it implicitly returns `None`. – Tom Karzes Jul 11 '23 at 02:44
  • Either `print` in the function, or `print` the return, but not both. As a general rule, a function like this should ONLY return its values, and let the caller decide what to do with it. Don't `print` in a utility function (except for debugging). – Tim Roberts Jul 11 '23 at 03:05

0 Answers0