Help me with factorial


#1



i dont understand the factorial excersise


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


#2

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


#3

i read it now how can i turn it into code


#4

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


#5

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


#6

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)
else:
return x

print factorial(4)


#7

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