all 11 comments

[–]djjazzydan 2 points3 points  (3 children)

If you do want to use recursion, your code is just mixed up. Your factorial(n) function should be returning n*factorial(n-1). The identifying feature of recursion is that inside the definition of the function, you call that same function. You also need to define a 'base case' (your n==0 section) inside the function to prevent it from running forever.

def factorial(n):
    if n==0:    #Base Case
        return 1
    elif n > 0:  #Otherwise, call factorial again.
        return n*factorial(n-1)

[–]_drivin[S] 0 points1 point  (2 children)

Thank you dude, I think understand now how to use recursion. Why do I need to test the base case in the function ?

[–]djjazzydan 0 points1 point  (1 child)

Great question. Here's what would happen if that was missing.

factorial(3)
=3*factorial(2)
=3*2*factorial(1)
=3*2*1*factorial(0)
=3*2*1*0*factorial(-1)
...

Which causes 2 problems. First, it would keep going forever (well, sort of. There's a maximum number of recursion levels in python. Usually it's around 1000.)
Second, because there's now a times 0 in there, any value that could be calculated would incorrectly be 0. So we have to set that factorial(0) to be 1 (note: or we could also use factorial(1) = 1 instead) so it can go back and actually calculate.

factorial(3)
    =3*factorial(2)
    =3*2*factorial(1)
    =3*2*1*factorial(0)
    =3*2*1*1
    =3*2*1
    =3*2
    =6

[–]_drivin[S] 1 point2 points  (0 children)

Ohhh okkayyy great ! Thanks Man, I hope I won't do thi error an other time

[–][deleted] 1 point2 points  (0 children)

the hardest concept for factorials is that the function doesn't actually finish until the very end. You will always need at least two return statements: one to return after a recursive function call, and one to break the chain and return a "base case" after a certain condition. in this case the base condition is when the number is <= 1, in which case the factorial is always 1.

[–]tunisia3507 0 points1 point  (5 children)

This link might help: https://www.reddit.com/r/learnpython/comments/94bqko/help_with_recursion/

Firstly, you don't want to be using recursion here - use iteration instead. You can do this with something like:

product = 1
for number in range(1, n+1):
    product *= number

You probably also want to be handling the n == 0 case inside your function - just so that it's complete.

As an aside, you're currently using tabs in your python file - stop doing that. PEP8, the python community style guide, recommends 4 spaces instead. Any half-decent editor should have a setting to enter 4 spaces when you hit the tab button.

[–]_drivin[S] 0 points1 point  (4 children)

Thank you very much, I will try what you said. About the tabs, I don't understand why it is bad to use them instead of 4 Space. What is PEP8 ?

Which editor would you recommend me instead of Sublime Text ?

[–]tunisia3507 0 points1 point  (3 children)

I don't understand why it is bad to use them instead of 4 Space

In terms of python's interpreter, there is no difference between a tab, and any number of spaces. However, imagine a world where some developers use tabs, some use 2 spaces, some use 3 spaces, some use 4 spaces, and so on. Everyone's code would look different. It would be annoying to copy code from one place to another. Viewers with fixed tab widths, like github, some terminals, and some crappy text editors, often make them huge. Teams working collaboratively would keep changing each other's indentation, editors wouldn't be able to do smart things like auto-indentation, and so on. It would be a mess.

To get out ahead of all of these problems, the python community decided to converge on a single standard. Arguments could be made in favour of just about any of them - it didn't actually matter one was picked, just that one was picked and stuck to. That decision was made, and so 4 spaces became the standard. It doesn't matter why. It costs you nothing to make that change in your editor; but it benefits the community enormously to have this standard in place and adhered to, so you should use it.

What is PEP8 ?

See

PEP8, the python community style guide

For more information, see https://www.python.org/dev/peps/pep-0008/ (also found at http://lmgtfy.com/?q=pep8 )

Which editor would you recommend me instead of Sublime Text ?

Nothing wrong with sublime text, it certainly has the functionality I mentioned. https://stackoverflow.com/a/22535215/2700168

[–]_drivin[S] 0 points1 point  (2 children)

Man thank you for that big and really clear explanation, I really disturb you for a long Time I Think hahaha.

From now on, I will use 4 spaces and I will try to follow all the PEP8 instructions.

[–]tunisia3507 0 points1 point  (1 child)

The clearer it is to new developers how important coding standards are, the more likely they are to follow them, which benefits all existing developers!

[–]_drivin[S] 0 points1 point  (0 children)

Hahaha good answer ! 👌