Factorial


#1

this is what i came up with, Is this the only way it can be done tho just curious. how did you do it.

def factorial(x):
    result = x
    print x
    multiplier = x-1
    print multiplier
    if x > 0:
        for i in range(x):
            if i > 0 and multiplier > 0:
                print i, "times"
                result = result * multiplier
                multiplier = multiplier - 1
                print multiplier, "in the loop"
            print result
            
    return result


print factorial(5)
print factorial(4)

#2

take a look at this topic here


#3

You can do this with recursion and make your life much easier.
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)


#4

I'm pretty sure my solution should work, but the exercise doesn't accept my answer.

y = 1
def factorial(x):
global y
for n in range(x):
    y = y * (n+1)
    print n, y
print y

#5

You are supposed to return an int not print. so do this

y = 1
def factorial(x):
    global y
    for n in range(x):
        y = y * (n+1)
    return  y
print factorial(6)

but its a Good Idea to keep everything inside the function. Because if y changes then the function will return the wrong value for any input. one suggestion is to do this

def factorial(x):
    y = 1
    for n in range(x):
        y = y * (n+1)
    return  y

print factorial(5)

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

mines looks too simple compared to yours, worked though