Help me with factorial


i dont understand the factorial excersise

this is what i got to so far
def factorial(x):
    for a in x:
        if int(a)==0:
            return 1
        elif int(a)==1:
            return 1
            total =total* int(a)*(int(a)-1)
            return total


Do you know what factorial is? If not, I suggest looking up on wikipedia, reading the first paragraph should give you all you need.


i read it now how can i turn it into code


Decide on what actions should be carried out and then write those as code.
If there's some action you don't know how to write as code, then you've identified a problem that you can research/ask about.

Do you know how to count from 2 up to some number? And how to multiply? Because that's about all the operations you need to compute factorials


yes i know how to do that
by the way thanks for all your help


Basically a function that calls itself (known as recursive functions). Makes sense when you don't know how many times the function needs to be called to get the answer. Like factorial (multiply the parameter number by every number below it until 0). Other examples might include finding the sum of every number below the given number until 0.
the item below evaluates to:
product of x and [factorial(x-1)] - which calls itself over and over again until the condition is met - that x = 1.

def factorial(x):
if x > 1:
return x * factorial(x-1)
return x

print factorial(4)


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