5. Factorial : please explain this solution


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

This is my solution, I used an example from another user but I don't understand why you have to set total = 1 or why the range is configured as it is. How does this affect the result?


Well, if you multiply something with 1, it stays the same, and you need a start value, so 1 is perfect

why not? You want all the numbers from 1 till x, but x is not included (it is lesser then x) so you have to add +1 so that x is also included


AH ! Thank you, this really clarified everything :]


still, you learn far less from this then actually write a solution yourself. It is like reading math solutions, and then at the exam you have a problem


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