Pls Help!

Hi All!

So, i have this question and i dont seem to figure it out. What am i doing wrong?

this is the question:
Fill in the blanks to make the factorial function return the factorial of n. Then, print the first 10 factorials (from 0 to 9) with the corresponding number. Remember that the factorial of a number is defined as the product of an integer and all integers before it. For example, the factorial of five (5!) is equal to 12345=120. Also recall that the factorial of zero (0!) is equal to 1.

and this is my code:

def factorial(n):
    result = 1
    for x in range(result,n):
        result = result * n+1
    return result

for n in range(1,10):
    print(n, factorial(n-1))

the original code:

def factorial(n):
    result = 1
    for x in range(1,___):
        result = ___ * ___
    return ___

for n in range(___,___):
    print(n, factorial(n+___))

and this is the error message i get:
Not quite. Pay attention to the operations that the code is
doing and make sure that the factorial function returns the
product of the numbers between 1 and n (including n).

I am stuck and my head hurts. i am sure it is a simple answer, but i can not see it. Can anyone help, please?

Hello @babygroot8236561007, welcome to the forums! Why do you have this:

Think about it. A the factorial of a number (x) is the product of multiplying every number before and up to x. For example, the factorial of 4 is:

4 * 3 * 2 * 1 = 24

What is your code doing?

If you're stuck, look at this:

That should be enough to see where you went wrong, but if want a full explanation of the code (spoiler-alert), then see below:

Code with explanation-factorial calculator:
def factorial(n):#defines a function 'factorial' with parameter 'n'
    answer = 1#sets the answer to one
    for i in range(1, (n+1)):#this is where the magic happens:
#This says, for every number in the range of 1 to `n+1`, execute the code. 
#The reason I used 1 as my #starting point is that `range()`'s start is inclusive. 
#This means that the first value of `i` will be the same as the starting value.
#Therefore, if I used 0 as the start, all I would get is zero.
#I use n+1 as the end, because the stop of a `range()` is exclusive.
#This means it will stop at the number before the number designated *(see below for an explanation)
#This iterates through all of the integers in that range, and for each integer, 
#executes the following code:
        answer *= i#multiplies answer by i (this is why I set it to one; because anything multiplied by 0 is zero)
    return answer#returns answer

Explanation of the `range()` function:

``` for i in range(0, 2): print(i) ``` This will print: ``` >>0 >>1 ``` The reason for this is that the first parameter provided (if there's two or more) is the start value, and is inclusive; it starts at that number, like when you count from 1, you start with `1, 2, 3, etc.`. The reason it stops at 1 is the second parameter is exclusive, the function doesn't return it; the last integer it returns is the one just before it. (Like when you stop at traffic lights-or any queue where there is a 'stop before this line' sign, you stop just before the line.) . I hope this helps!
def factorial(n):
    result = 1
    for x in range(1,(n+1)):
        result *= x
    return result
for n in range(0,10):
    print(n, factorial(n-1))

so i made these changes, but i still get an error…is the same if i change (0,10) with (0,9)…
i am still doing sometghing wrong

Hello. What error do you get? Is it an actual error caused by Python (i.e. it doesn’t like your code), or is it a CC error (i.e. your code doesn’t do what CC wants)?

sorry, forgot to put it as well.

error:

Not quite. Remember that we want to print the first 10
factorials, starting from 0 to 9.

Factorial zero is 1. However, in multiplication that is not a certainty. Multiplication by zero is zero. That much is.

There is a good reason to start with 1 in the range.

Hello. You are mostly calculating the factorial correctly, but the problem is with this, as well as what @mtf said(thanks @mtf. I wouldn’t have noticed that if you hadn’t picked it up!) is this:

Specifically, the n-1. Essentially, what you are doing here, is saying for a number n between 0 and 10 (let’s say 2), print the factorial of 2-1 (n-1). In our example of two this is what it will print:

>>1 #The factorial of 1 (2-1 or n-1)

It should, however print:

>>2 #(The factorial of 2)
Hint if you're really stuck, but I'm confident you won't need it!

Remove the -1, so it reads:

print(n, factorial(n))

That way, it will print the factorial of n, not n-1. If n is 2:

>>2

This is because you aren’t subtracting 1 from n before you print it.

I hope this helps!

1 Like

What gets me is why anybody would teach that a range is the appropriate tool to use. when it is so clumsy and cumbersome in this application. Nothing eloquent or practical about it.

Why not, while? We have a value we can wind down to zero, so why not do that? It’s straight forward with no unnecessary code burden.

f = 1
while n > 0:
    f *= n
    n -= 1
return f

The whole purpose of using range in this problem totally escapes me.

hi, and thanks for trying to help me!

I am trying to learn python on my own, and tried to practice it by doing many exercises…and i am stuck to this one. Why it says so, i dont know, all i know is that i can not move on without answering this…and it sks!

Do you mean you are having trouble filling in the blanks?

yes i am… :frowning:

Well you have filled in the blanks correctly in your factorial function, just not in the loop that follows. It should start with 1 not 0.

I don’t see what’s clumsy about it

factorial = lambda n: reduce(mul, range(2, n+1), 1)

it fills a role in the task, there’s no undesired behaviour to work around
to compute factorial you need a range of numbers… use range, right?

1 Like

Ignore the blanks, look at what information you have and consider how those pieces necessarily must be combined.

Do you know how to carry it out manually? Good, you already have a reference for what goes where.

Can you write a program that counts upwards in increments of 1? … So you’d do that, and print them out.
Are those the correct numbers to be using? Adjust the start/end until it is.
Then what do you do with them? You multiply, no? So do that. You need to hold on to the current result, so you’d use a variable.
When you’re done, when you have the result, you hand it back to the caller.

In particular, don’t view it as one big process which goes wrong no matter what you try.
Instead you consider what the first thing is that should happen, write code for it, and then move on to the next thing.

…observe what’s being done, compare to what you mean, adjust. repeat until done.