#python - beginner
8 messages · Page 1 of 1 (latest)
factorial(4) would be 1 * 2 * 3 * 4
factorial(13) would be 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13
haha that seems to be part of it, but I think its the loop part that still confuses me. I seem to get all the values from 1-4 and not only 4.
fact = 1
for i in range(1, n+1):
fact = fact * i
print(fact)
factorial(4)```
gives me
1
2
6
24```
I want it to only print 24, how do I make it skip the others
There are a couple of ways, you could print after the loop but still inside the function. Or have the function return fact after the loop
# Option 1
def factorial(n):
fact = 1
for i in range(1, n+1):
fact = fact * i
# Print once after loop
print(fact)
factorial(4)
# -----------------------------
# Option 2
def factorial(n):
fact = 1
for i in range(1, n+1):
fact = fact * i
# Return after loop
return fact
fact = factorial(4)
print(fact)
super helpful, thank you! 
didnt know the indent made such a difference