5. Factorial : please explain this solution


#1


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?


#2

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


#3

AH ! Thank you, this really clarified everything :]


#4

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


#5

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