Factorial | Help


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

What is wrong with my code ?
It says: "Oops, try again. factorial(4) returned 12 instead of 24"


a function ends the moment a return keyword is reached, that is important to realize. In case of your while loop, in the first run of the while loop a return keyword is reached

Also, this doesn't work:

product = x*(x-1)

now, everytime product gets assigned a new value from x, but the total of product is never stored. In the beginning of your function, create a product variable and give it a value of one. then update the product variable every time its need to be updated:

product = product + ??

otherwise, no total is scored


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