#python - beginner

8 messages · Page 1 of 1 (latest)

umbral wolf
#

Is it that you don't know what factorial means in math?

#

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

forest berry
#

I want it to only print 24, how do I make it skip the others

umbral wolf
#

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)
forest berry
#

didnt know the indent made such a difference