5. Factorial


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/5?curriculum_id=4f89dab3d788890003000096#


Oops, try again. factorial(2) returned 1 instead of 2


def factorial(x):
    factum = 1
    while x > 1:
        for i in range(1, x + 1):
            factum = factum * i
            return factum 
    else: 
        return 1


#2

Unindent your return factum so that it lines up with your for loop. like so:

for i in range(1, x + 1):
    factum = factum * i
return factum

#3

this one works

def factorial(x):
    result = x
    reduced = x - 1
    while reduced > 0:
        result = result * reduced
        reduced -= 1
    return result

print factorial(9)

def factorial(x):
result = x
reduced = x - 1
while reduced > 0:
result = result * reduced
reduced -= 1
return result

print factorial(9)


#4

def factorial(x):
if x==0:
return 1
else:
return x*factorial(x-1)

=>este tambiƩn funciona...estoy ocupando recursividad


#5

I don't understand it.


#6

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