Factorial (helplessly lost)


Here's the exercise.

I am so frickin lost. I understand what it's trying to get me to do. I just have absolutely no clue how to execute it. I've been trying forever and I'm about ready to quit tbh. Is there anyone who can walk me through it without just giving me the answer? I feel like I keep going in circles and I just need a guiding hand. We can start at the beginning cause nothing I've done seems to work


Of course, as I'm in the codecademy labs it gives me the flipping answer :rage:

But I really want to understand how/why it works. I'm so confused.


We know a factorial multiplies from high to low, going down 1 each time. Unfortunately, a for-loop goes from low to high. So how would you go from high to low?


Ummm, rearrange the list?


Or incorporate n-1 some how but I don't know how


and wouldn't multiplying low to high come up with the same answer anyway?

1*2*3*4*5 == 5*4*3*2*1



A list would over-complicate things a bit.

Although, you're right xD I guess I over-complicated mine a bit.

I had used a while loop, but a for-loop can work


All that matters is getting from point a to b while multiplying everything in-between and storing that data to then be returned.


In the hints it said to consider calling factorial(n) on itself?

How can you do that if you haven't defined the function yet?


Also what is a recursive function? I was looking over the forums earlier and it mentioned them but I don't think we've learned about those in the course?


That's recursion. Recursion is like a more advanced loop that can be very useful in certain cases. I can see it working here too. As long as you have a break case, (in the hint it's 1) it will call itself over and over until the break case is reached. If you had function(x) you can call function(x-1) inside of that which will call that function(x-1) before moving on to the remaining code inside of function(x). The remaining code will be executed once a break case is reached and the computer will start working in reverse (First In, Last Out). They haven't taught them yet, which is odd that it would be in the hint.

This article might be helpful: Recursion


Okay so there was a lot of stuff in that article that was way over my head. However I did come up with this:

def factorial(n):
    if n < 2:
        return 1
        return n * factorial(n-1)

Which is infuriatingly more simple than the crap I was trying to do before. And it works so woo! But I don't quite understand why. I don't get it how it's iterating over every number in a range (1-n) without a loop?

Like if I called factorial(5) wouldn't it just give return 5 * (5-1)? When I run this it gives me the right answer of 120.


oh wait wait I get it.

if you were to write it out it would look like

5 * factorial(5-1)

5 * factorial(4)

5 * (4 * factorial(4-1))

5 * (4 * factorial(3))

5 * (4 * (3 * factorial(3-1)))

5 * (4 * (3 * factorial(2)))

5 * (4 * (3 * (2 * factorial(2-1))))

5 * (4 * (3 * (2 * factorial(1))))

5 * (4 * (3 * (2 * (1)))) <<---- since factorial(1) returns 1


Jesus that's hard to wrap my head around but I get it now.

Thank you!! I didn't realize you could call the function within itself.


Well, I just scrapped my post xD
Yep, you got it!
Whenever I use recursion, I like to keep a pen and paper handy to keep track of it.


What was your solution with a while loop? Just curious.


def factorial(x):
    total = 1
    while x > 0:
        total *= x
        x -= 1
    return total


That's super interesting! I tried something similar when I first started but it threw an error at me when I tried to use *= saying it didn't recognize it or it was a syntax error or something.


You could also do:

def factorial(x):
    total = 1
    if x > 0:
        for num in range(1, x + 1):
            total *= num
    return total

hmmm... maybe Codecademy just wanted a different way? Not sure. Either way, I can ensure it does work, at least in Python 2.7


That looks very similar to what I tried and it didn't like it. Oh well. Thanks so much for your help!


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