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))
1

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. Thank you so much. Knew I was missing something easy. Appreciate it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.