Practice makes Perfect: factorial


#1


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


Hello all, hoping to be pointed in the right direction if possible.
I'm not quite sure why my code is throwing an error... "Oops, try again. factorial(3) returned 3 instead of 6"

I have a suspicion that there may be something wrong with the way I've denoted 'i' in range() so that it doesn't take all numbers leading up to x into account?

Many thanks in advance


def factorial(x):
    total = 0
    if x > 0:
        for i in range(x):
            total = x*(x-i)
    elif x == 0:
        return 1
    return total


#2

You range starts with 0 and goes upto x - 1.
also your value of total is changing at each iteration so its not able to catch all multiplications.

You can initiate total with 1 and make changes in range so it should start with 1 and goes upto x?.
and at each iteration multiply and store i's to total?

(if you do as I mentioned,You will have to no need to add else part ,your total will have 1 and it will satisfy for 0! too)


#3

Hello - thank you for the reply.
Have just been trying to work on the code with your comments.

def factorial(x):
    total = 1
    if x > 0:
        for i in range(1,x):
            total = total + x*(x-i)
    return total

I'm not 100% clear on storing i's to the total?
Apologies if I'm being dim, I've been coding in another language for the past month so have evidently pushed out some python knowledge in that time. :slight_smile:


#4

point1.
We need to fix range
As I said if we try that way (mentioned in first post) then we have to go from 1 to x (including x).

Now in python 2.0

range(1,3) ---> [1,2]
range(1,4) ---> [1,2,3]

I hope you get the hint.

Note :

4! = 1 * 2 * 3 * 4
5! = 1 * 2 * 3 * 4 * 5
.....
n! = 1 * 2 * 3 * 4 * ... * (n -1) * n

it means we have to include final number too

Point2.

I'm not 100% clear on storing i's to the total?

suppose I have to go over a number and add (you have to multiply as we're calculating Factorial) all upto that number (starting from 1) I'd do this..

x = 6
total = 0
for i in range(1,x+1):
    total = total + i
print total #will print 21

You can see I also implemented point one in this code.


#5

The answer could be right in the Hint, which says:

Consider having factorial() call itself. When the input is 1, your function could just return 1. Otherwise, it could return the number multiplied by factorial(n - 1).

So instead of multiplying for x - 1, you could call the function itself with a x -1 as an argument and multiply x by it.
Also, you could say that if a number equals zero or one to just have it return 1 and then otherwise just go on the multiplication.
Finally, the for loop might not be necessary at all. Hope this coud help.

Cheers and happy coding!


#6

Hello,

That is amazing, it's worked. Thank you.
However, I don't think I 100% understand how the calling the function bit works - please may you explain how it works?


#7

Sure,

So first you tell the function that if x equals 1 or x equals 0 just return 1.
Now you create an empty list and add to the list the result of the multiplication between x and function_itself(x-1).
All there's left to do is return the result of the numbers inside the list, but added together.

Hope this helps!


#8

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