So this returns 12 for factorial(4) instead of 24, which indicates it is not looping, but instead just multiplying by x-a once, but I cannot understand why.

def factorial(x):
for a in range(1, x):
result = 0
result += x*(x-a)
return result
if x <= 1:
return 1
print factorial(4)

Setting the result outside the loop doesn't seem to solve the problem. Nor does putting the if statement first. I tried a while loop but couldn't get it to work either. Can it work with this for loop or am I barking up the wrong tree?

To get a good picture of where factorials are used, combinatronics is one where is it is woven into the mathematics.

Consider this problem...

Given 10 colored marbles, all unique, how many combinations taking 3 at a time can be chosen? The three are put back in each time so there are always 10 marbles..

I'll leave this with you.

You can test your factorial function with this program:

If you get the following output, your program works correctly, at least at this point. There are lots of ways to foul a program as you will discover in your travails.