Factorial( pls help)


#1



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


Error : File "python", line 11
else :
^
SyntaxError: invalid syntax


I expect it to work properly, cannot find the solution


def factorial(x) :
    count = 1
    sm = [[count]]
    fc = 0
    while count == x :
        for x in sm :
            for i in x :
                fc *= int(i)
            break        
    return fc            
    else :
        sm.append(count)
        count += 1
    return count    
    return sm


#2

I believe you have reached this error because you placed your return statement outside your for loop, making it apear as an else with out if for or while.
Assuming you intended this else to be for the while part of your code it shoould look like this:

def factorial(x) :
    count = 1
    sm = [[count]]
    fc = 0
    while count == x :
        for x in sm :
            for i in x :
                fc *= int(i)
                return fc    
            break        
    else :
        sm.append(count)
        count += 1
    return count    
    return sm

#3

simple the better

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

print factorial(5)


#4

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

#5

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