FAQ: Learn Python - Practice Makes Perfect - factorial

Community%20FAQs%20on%20Codecademy%20Python%20Exercises

This community-built FAQ covers the “factorial” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise factorial:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

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!
def factorial(x):
    total = 1
    while n<=x and n>0:
        total *= n
        n+=1
    return total
  
print factorial(5)

what is error in this code?
and if there is an error tell me how to make factorial in which multiplication starts at 1 and goes till x.

the error is pretty helpful?

i would do it the other way around, go from x till 1. Its easier, given you already have x (the parameter)

I found this online and kind of get it. However, the loop of the whole function is sending me a bit mad. Could anyone explain how this works:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
print factorial(5)

Here is my original function, I can’t figure out how to make it right:

def factorial(x):
  fact = 0
  while x > 0:
    fact = x * (x - 1)
    x = x - 1
    return fact
print factorial(5)
1 Like

My issue with the first one is the first part would be multiplying 5 by the factorial of 4, but this hasn’t yet been defined.

recursion, yippie. Here are some good answers about recursion:

Recursive function calls

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.

It has been defined, the function exists

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)

Anything after return is unreachable. Swap those two lines around, or print the return value at the caller.

print (factorial(5))
2 Likes

Why is this not working?

def factorial(x):
  total = 0
  while x > 0:
    total = x * (x-1)
    x = x - 1
  return total

print factorial(2)

Gives error code “factorial(1) returned 0 instead of 1”

this line:

total = x * (x-1)

lets fill in the values:

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)

1 Like
def factorial(x):

  total = 1

  while x>0:

    factorial = total*x

    x-=1

  return factorial

What is the error in this code? It says “factorial(2) returned 1 instead of 2”

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?

Hi!
After I ran my code it signed to me like I succeed, but I don’t see hoe the code is make sense and does what I want him to do.

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:

pythontutor.com/visualize.html

to see the problem.

OK, so I have this:

CodeCademy accepts it, but as soon as I change the first print into return, I get the wrong value and I just don’t get it.

thank you in advance!

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
>>> 

Do not you think we should be able to this as well:

def factorial (x):
multiplication = 1
string_n = str(x)
for char in string_n:
multiplication *= int(x)
return multiplication
print factorial(5)

What do you think about the code above?
Thanks

How many characters are there in, str(5)? Trick question. Answer: 1. So your code will return, 5 when it should return, 120.

5! == 120