Define a function factorial that takes an integer x as input.
Calculate and return the factorial of that number.
Here is my attempt:
def factorial(x): result = 1 if x == 1: return result elif (x - 1) > 0: result = result * x * (x - 1) x = x - 1 return result for num in x: #Do I loop here or earlier? result = result * x else: return result print factorial(x)
It works for x = 1, x = 2, and x=3. However, I am stuck on how to create the loop for x = 4, 5, …
Do I replace the if statements with a loop? Generally, when do I use if/elif/else and when do I use loops?