2

I'm sure I'm making a simple error but in my function looking to return the factorial of a user given input when the input isn't 1 or 0 it actually just returns none. I printed out what it was doing within the helper function and it looks like it obtained the proper result but when I run it as below it returns none. Please let me know what obvious oversight I'm making. Thanks.

def recurseFactorial(num, mySum):
    if num==0 or num==1:
        return mySum
    else:
        recurseFactorial(num-1,mySum=mySum*num)

while True:
    myInput = input("Please enter the number: ")
    print(recurseFactorial(int(myInput), 1))
MPython
  • 23
  • 2
  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – VLAZ Nov 03 '22 at 17:35

1 Answers1

3

In the else branch, you're calling recurseFactorial, but you're not returning its result. And since that branch has no return statement, it returns None by default.

You want this instead:

def recurseFactorial(num, mySum):
    if num==0 or num==1:
        return mySum
    else:
        return recurseFactorial(num-1,mySum=mySum*num)
John Gordon
  • 29,573
  • 7
  • 33
  • 58