Why this doesn't work?
Return should be outside the loop.
Your code is correct. Placing the return outside the elif function will return the answer.
But there is an easier way. Try using a while loop:
import math def factorial(x): # make sure the input is a positive number: x = abs (int (x)) # create a variable with status x ans = x # subtract 1 from x! x = x - 1 # create a loop that subtracts one from x after each iteration: while x > 0: # make the calculation visible to check it. print ans, '*', x, ans * x ans = ans * x x -= 1 # return the answer after the loop return ans print factorial(10)
for i in range(1, x+1):