5. factorial



Oops, try again. factorial(2) returned None, did you remember to return the result?

I always get this Error message although print total always shows the right answer. Could someone tell me why and how to fix it?

def factorial(x):
    if x == 0:
        return 1
    elif x == 1:
        return 1
        i = 1
        total = 1
        while i < x+1:
            total *= i
            i += 1
    print total
print factorial(5)


You're not returning anything. You're just printing the total.


ah sweet thank you.

on a scale from 1 to 10, how overly complicated is my code? :unamused:


Well...I think you can ditch your if and elif statement because you already set your total = 1. With this, the function will return 1 if x is 0 or 1.
As for the choice of loops, I prefer a for loop but a while loop is fine.

Someone else may have another feedback.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.