Factorial Help


#1

This is my current code which is returning the following error message: "factorial(3) returned 8 instead of 6"

def factorial(x):
    if x==0 or x==1:
        return 1 
    elif x==2:
        return 2 
    else:
        total = x 
        while x >= 1:
            total = total * x-1
            x=-1
        return total

#2

keep in mind order of operations, multiplying happens before subtracting. so this currently what is happening:

total * x
total -1

if you want to subtract one of x first use parentheses


#4

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