I'm not sure I understand factorial (5/15)



I have been stuck on this exercise for quite a while now, and I think this may be because I don't fully understand the purpose of the function. I have tried looking in the hint and at other posts, but I don't understand how to do this.
Couldn't just do

def factorial(x):
return factorial(x)

Thanks for any help

EDIT: I know there is supposed to be an indent in front of return, but it isn't working for me in the forum for some reason.


the purpose of the function is to return the factorial of a certain number, you know what factorial is? It is explained in the lesson, or you could find external source.


It's all the numbers before the number you want a factorial of multiplied together, e.g. factorial(5) would be 5 x 4 x 3 x 2 x 1, right?


so, now you understand what you function should do:

def factorial(x):
  // magic happens here
print factorial(5)

so now, 120 should be printed. You will need some kind of loop, to do the multiplying. and maybe a variable to store the result?


You actually did it, except your second line needs indenting.

Your function just calls itself endlessly atm, there is no way it will ever return, it just calls itself.
Factorial can be easily computed if you know what the factorial for one less is, which you can use your own function to compute, except when the value is 0 in which case you should return without calling self first, and now that it returned, the previous calls can finish as well and you end up with a result.


Ok, so I don't really understand how the hint works - why factorial(n -1)?


If you know the answer to factorial(n-1), then what do you need to do to get the factorial of n?

What you're meant to do is, each time your function is called, it should make the problem slightly smaller (n-1) and call self with that simpler problem. It has to get a little smaller each time, and eventually reach a trivial case. When the function is called with the trivial case, you can return the result immediately and then the previous calls can finish. When the call with n-1 has finished, your function should use that result to figure out what its own result is.


Ok, I think I understand. I'm on my phone atm, so I can't post the code, just yet.def factorial(x):


def factorial(x):
    result = 0
    for i in range(x):
        x * factorial(x-1)

Am I on the right track?


Looks like you're writing code without knowing what you want it to do.

Decide on what it should be doing before you bother writing anything. Convince yourself of that your idea will produce the correct result, and only then start writing.


def factorial(x):
    if x == 1 or x == 0:
        return 1
        for i in range(x):
            return x * factorial(x - 1)

So I finished the exercise with this as my code, but I don't understand how that last line works. If the function is included within the function, doesn't it keep calling itself over and over? I tried

x * (x - 1)

as the last line, but for some reason that didn't work.
As far as I can see, it would be for example 4 * factorial(4 - 1). So what does the factorial do, and how does the code know what it is?
Thanks so much


That is an incredibly pointless loop. How many iterations does it do?


Which one? The one without factorial? That works until 4, where for some reason it returns 12 instead of 24. The one with factorial works enough for it to let me pass. Sorry if I'm annoying, I'm just an ultra beginner.


The last one that you posted. Your loop has absolutely no effect, you can remove that line with no difference in behaviour.

It's not a loop if it iterates once. That's the behaviour that a line has without being in a loop.

Removing redundant code leaves this:

def factorial(x):
    if x == 1 or x == 0:
        return 1
    return x * factorial(x - 1)

So ask yourself what the loop was meant to be doing because you really need to re-think that


Wouldn't that just do e.g.
5 * 4
instead of 5 * 4 * 3 * 2 * 1?
What does factorial in the last line mean? Isn't that calling the very function I'm defining, which, if my thinking is correct (which I'm not sure about), would result in an infinite loop, right? I though the built in function was disable for this exercise. I thought the loop would keep taking 1 away from x, so that it would keep multiplying by one number less than last time, if that makes sense.


Factorial is a function that computes the factorial.


Not if it eventually returns, no

There is a library function for factorial, but that has to be imported. It isn't disabled, no. But you're not importing it either, are you?

You didn't have a loop.
This is not a loop:

while True:
    print 'not looping.'

It runs once. The whole point of a loop is to run more than once.


My solution with a loop, not with recursion


def factorial(x):
if x==0 or x==1:
return 1
while x!=1:
return fac

try this it worked for me!!


Hi, sorry for taking so long, I had exams. So the thing that I don't understand is how you can call the function you are defining within the function itself?


You're not defining the function when it is called. The function has been created, otherwise it could not have been called in the first place.

All you need to do is make sure it keeps making the problem smaller each time, so that eventually it can start returning.