5. factorial


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/5?curriculum_id=4f89dab3d788890003000096#


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
    else:
        i = 1
        total = 1
        while i < x+1:
            total *= i
            i += 1
    print total
    
print factorial(5)


#2

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


#3

ah sweet thank you.

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


#4

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.


#5

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