Factorial


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/5?curriculum_id=4f89dab3d788890003000096#

Oops, try again. factorial(1) returned 0 instead of 1

I have defined first what to return what to do when 0 is being entered so I'm not sure why I keep on having this error


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


#2

Oops, try again. factorial(1) returned 0 instead of 1

So the question is, how your code works when x = 1.


Your function starts with the if, let's check the condition:

1 == 0 => False

, so we go to the else block.


Now we have a while loop. In the first iteration value of x will be changed to:

x = x * (x - 1) = 1 * (1 - 1) = 1 * 0 = 0

And now condition of while is no longer True:

0 > 0 => False

, so we exit the while loop.


And now we have the last statement, return x, current value of x is 0 and this value will be returned.


So, something is wrong in the else block :wink:


#3

very helpful you break it down like that, thanks

I changed the code quite a bit and I am still off but I am stuck again

def factorial(x):
total = []
while x > 1:
x = x - 1
total += x
for number in range(len(total)):
number * number
return total

so the plan is to add each number into a list, total to then multiply every number in the list.

however there are some issues. I will explain with an example.

let's say we have entered number 4.

what it will do instead of entering 4 first is instantly -1 and start off with 3. then we have a list [1 2 3] so the 4 is missing. however I am not sure how to first add 4 and then substract it by 1.

then I still have the problem with a factorial 1 or 0 and now I have another error:

Oops, try again. factorial(1) returned [] instead of 1

any more pointers.


#4

Good job :slight_smile:

When you add something to the list you should use append method. To solve this problem you can simply change order of operations in the while loop, first add to the list and then decrease value of x:

while x > 1:
  total.append(x)
  x = x - 1

Now total will not include 1, but we want to include 1 always, even when the x > 1 is False, right? So we can initialize total in this way:

total = [1]

But now, the question is, why for the x = 4 you want to get the list [4, 3, 2, 1]? I know that you want to multiply every element of the list and return this value, but why not just calculate 4 * 3 * 2 * 1, you had these values in your hand because you have added them to the list.

So instead of initializing total with [1], you can simply use 1. And instead of adding elements to the total you can change value of total by multiplication:

total = 1
while x > 1:
  total = total * x
  x = x - 1

#5

very well explained. it's so much more difficult to fully think out the problem. I also even completely forgot to think about .append(). I feel codecademy is still basic but I find it very difficult!


#6

Codecademy courses are very basic. Codecademy teaches how to write the code (syntax), not how to program, create things.

This exercise is a little different because you have to come up with the idea how to solve this problem.


It's hard to learn how to write algorithms. It requires straight, logical thinking. Humans (evolutionary) are not very well adapted to this task.

My advice is to try to solve few algorithmic problems. You can find many, interesting problems in the Project Euler. Before writing any code try to come up with the idea how you would solve this on a smaller scale without using a computer.

Solution proposed by me to the factorial exercise is a computer representation of how we calculate factorial on the paper, right?


#9