```
def factorial(x):
while x > 1:
y = x - 1
answer = x * y
answer2 = answer * y
return answer2
print (factorial(5))
```

can someone please help me i cant understand how to do this

```
def factorial(x):
while x > 1:
y = x - 1
answer = x * y
answer2 = answer * y
return answer2
print (factorial(5))
```

can someone please help me i cant understand how to do this

before you while loop, i would create a variable (call it total for example), and give it a value of `1`

. then inside the loop, multiply total by the number (`x`

), until (you can use the while loop like you do now) x is one (reduce x by one in each run of the loop

then, after you loop is finished return the total

A while loop keeps working as long as the condition is met, in this case x>1.

The nature of a factorial is that it multiplies x with x-1,x-2... all the way till 1.

4! (Which is the factorial of 4) is 4*3*2*1=24.

First you say: While x > 1, which is good, that works well.

When you look inside the loop, you will see that the x never reaches the point where it will reach 1.

In your loop, x stays the same. The while is therefore indefinitely valid, and infinite.

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