Factorials


#1

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

I'm pretty sure I did it right, but I get an error message that says - "Oops, try again. Factorial(4) returned 12 instead of 24.

def factorial(x):
    for i in range(x):
        if x > 1:
            product = int(x) * int(x - 1) 
        else:
            product = 1
    return product

#2

Well you either return 1 or x*x-1 so in your case 4*(4-1) = 4*3=12.
The idea of factorial is that

n! = n*(n-1)!

meaning instead of x*x-1 you would return x* factorial(x-1).


#3

Agh I can't believe I made such a stupid mistake! Thanks a lot


#4

Would have liked to see someone mention the word recursion here.


#5

Yes the implementation of this is most easily done by a recursion. Do you know that word and just want to mention it or do you want an explanation for it?


#6

I know the word and wanted to mention it.