# Practice makes perfect: debugging factorial code

Hi everyone!

I’m new to this forum, but very happy I found it! I’m also new to coding, so I still have much to learn.

I was trying to solve the factorial problem of the loops module ‘Practice makes perfect’, but didn’t get the result I wanted. Of course, I have seen the solution by now, but I’d like to understand why my code doesn’t work, so I don’t make mistakes like these in the future! Anyone that could help me?

``````def factorial(x):
total = 1
for number in range (1, x+1):
total *= number
print factorial(6)
``````

Hello @method0049271172, welcome to the forums! The `return` being inside the loop as it is means that the function (and loop) will end after only one iteration. That means the first value of `total` will be returned. So, if you have `factorial(6)`, you will get:

``````total *= 1
``````
1 Like

Looks like you are pretty close to the solution.

The problem seems to be this line:

``````    return total
``````

`return` can be taken pretty literal, it literally returns/hands back data to the caller/function call. Which signals that the function is done executing

given the `return` keyword is reached in the first iteration of your loop, your loop only makes a single iteration. `return` will simple “break free” of the loop, given the function needs to end

kind of like making a test/assignment, handing the test/assignment in (return) doesn’t mean all the questions are answered (not all loop iterations are made)

2 Likes

So simple! Thank you so much for the quick reply!

1 Like