Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post ().
Other FAQs
The following are links to additional questions that our community has asked about this exercise:
This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
As for your original solution, the return keyword is nested in the loop.
fact variable will be re-assigned each iteration of the loop, so it will just contain the value of the last multiplication. We need to increase fact variable.
I did it this way, and “passed” the conditions Codecademy was looking for, but for some reason, the console blanks out (as seen in screenie below). Is this a bug?
Feels like there is something wrong with my code. I tried other online Python interpreters and got the same result (nothing in output).
def factorial(x):
total = 1
factors = [x]
y = x
if x == 1 or x == 0:
total = 1
return total
else:
while y > 0 and y != 1:
y -= 1
factors.append(y)
for num in factors:
total *= num
return total
print total
factorial(5)
total = 1 * (1 - 1)
# resolve the brackets
total = 1 * 0
i see a problem here. Furthermore, you just re-assign total each time, so total will just contain the value from the last multiplications. For example, factorial(5) gives 20 (5 * 4) while it should be 80 (5 * 4 * 3 * 2 * 1)
If you’re not aiming for recursion don’t use the function name inside the function, it’s asking for trouble. Even more so since you’ve reassigned that name to a variable.
The error in that code is saying that calling factorial with the argument 2 is returning a value of 1. There’s an error in logic in the code itself. As that function stands calling it with an integer large than zero will always return 1. What’s total for? Does it ever change?
This code is not correct, as you can see by the error on the right hand side.
NoneType is the absence of a return value, So after having reached the base case (x == 1), all the function calls are resolved. but a is never returned.
I recommend to step through your code with this tool:
That is a lot of logic for such a straight forward operation. The only conditional would be to make sure that x is not less than zero since factorial is positive by definition. Factorial zero is 1, also by definition.
One issue that we can see in your code is a return within the loop. That forces an immediate exit from the function so the loop never gets to run to completion.
I would start by making total = 1 rather than x. That will permit us to catch the edge case where x is zero since the loop will not run and it lets us return total but once, at the end of the loop.
>>> def factorial(x):
if x < 0: return False # per the first rule
total = 1
while x > 0: # per the second rule
total *= x
x -= 1
return total
>>> factorial(7)
5040
>>> factorial(0)
1
>>> factorial(1)
1
>>>