5 Factorial HELLP


def factorial(x):
if x<0:
return "none"
elif float(x) != int(x):
return "none"
elif x==0:
return 1
elif x>1:
while (x-1)>0:
return x*factorial(x-1)
return 1

when I submit this by itself it tells me that factorial(1) gives None as an output.
If I write print factorial(3) and submit it gives me errors on lines 10 and 13


You can't call a function from inside itself. It makes an infinite loop of confusion lol. Could you post the link to the specific problem that you are working on so I can get a better idea of what you are being asked to do.

If it is what I think it is than here is my code:

def factorial(x):
result = 1

for i in range(0,x):
    result = result * (i+1)
return result


This is called recursion, which is a valid form of programming. So you can in fact call the function from within itself so long as you pass in new parameters. It is important to have a base case that when reached will terminate the recursion and go through the return stack.

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


Thank you for the clarification. I am familiar with recursion as I have used it before. I just was not being specific enough in my response. In this particular case, recursion seemed incorrect but feel free to correct me if I am wrong.


Thanks everyone, but I figured it out a while ago (I think).


For beginners recursion will not be the go to approach. Most people won't be familiar with it and will instead look for an algorithmic approach. In the case of factorial, recursion is not necessarily the most efficient or fastest method, either.

Your approach above is simple and efficient. I would only change a couple of things.

def factorial(n):
    n = abs(n)           # n must be positive or 0
    if n < 2: return 1   # 0! and 1! are both 1
    result = 1
    for i in range(1, n):
        result *= n + 1
    return result


Is there a way to complete this exercise using the while function?

if n < 2: return 1
result = 1
while n > 1:
    result *= n
    n -= 1
return result


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


What if x is 1?

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


def factorial (x):
    return reduce(lambda a,b: a * b, range(1, abs(x)+1)) or 1


When x is 1 the code will return 1*factorial(0)=1*1=1
maybe I should add:
elif x<0:
print "Please, input a non-negative integer"

because according to condition of the task we need to calculate only them


Brain fart, on my part. You are right. What I meant to ask was, 'what if x is negative?'

We could protect the program from invalid inputs by making it a dependency of another, say, get_factorial() and validate inputs before calling factorial(). This way the function can be left pure. Same thing in, same thing out. Nothing asked for outside and nothing changed.


def factorial(x):
result = 1

while x != 1:
    result *= x
    x -= 1

return result

Note - I did != 1 instead of 0, as 4 * 3 * 2 is same as 4 * 3 * 2 * 1. No need to include 1.


Yes, this could be better


Does not address the negative inputs problem.