Code not working 5.factorial


#1



def factorial(x):
    m=x-1
    while m>0:
        t=x*m
        return t
        m-=1
    else: 
        return 1


#2

Much simpler to do this:

def factorial1(n):
    if n < 2: 
        return 1
    return n * factorial(n-1)

You see, factorial 0 and 1 both equal to 1, so this line takes care of that:

 if n < 2:

Then any other factorial is take care of by doing this:

  return n * factorial(n-1)

Hope that helps! :slight_smile:

-- @chipjumper36731


#3

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