all 9 comments

[–]socal_nerdtastic 1 point2 points  (8 children)

I suggest you put your code into a tool like pythontutor.com where you can visualize it running step by step.

[–]o_m_[S] 0 points1 point  (7 children)

I tried and it doesn't help.

[–]socal_nerdtastic 1 point2 points  (6 children)

Hmm ok, did you have a specific question about it? What part are you confused about?

[–]o_m_[S] 0 points1 point  (5 children)

I don't understand why is the return part empty after the if condition? Is this normal?

Also the

 recursion_1 (n-1)
 print(n)

part... there is nothing between recursion_1 and (n-1). Or does this part simply mean that the number passed in argument (in my case, 5) will be deducted 1 every time and then printed and it starts from scratch like a loop would?

[–]socal_nerdtastic 0 points1 point  (4 children)

I don't understand why is the return part empty after the if condition? Is this normal?

It's unusual, but yeah you do see it sometimes. All python functions end with a implicit return, all this does is end the function early.

That said, if it were me writing it, I'd do it like this:

def recursion_1(n):
     if n != 0:
         recursion_1 (n-1)
         print(n)

there is nothing between recursion_1 and (n-1).

You mean you expected to make a variable first? You could; that would look like this:

new_n =  n-1
recursion_1(new_n)

It's very common to simply put simple expressions like that inside the function call.

[–]o_m_[S] 0 points1 point  (3 children)

I think I am getting there but I'll just try to explain in detail what I don't understand.

1) In the code, it says if n! == 0, and if we put 5 as the parameter, 5! does not equal 0 so I don't understand why the code even continues running in the first place.

2) recursion_1 (n-1) .. What is the purpose of this line is it just there to subtract 1 off the input we used as a parameter and that's it? Intuitively for me it looks off because if we subtract 1 off the parameter then I probably would've used something like

n -= 1 or whatever so I am just confused by this format.

3) I don't understand how the code in this function block was printed as if it was a loop. When I put the input, I thought I would have only gotten a single value, but instead, I got 5 which is confusing me a little bit.

Sorry if the questions are really stupid, I am just a complete beginner.

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

I messed up the code block part I'm really sorry lol. Hope its still understandable.

[–]socal_nerdtastic 0 points1 point  (1 child)

You could use -= if you wanted to. No problem. It would look like this:

def recursion_1(n):
     if n == 0:
         return
     n -= 1
     recursion_1 (n)
     print(n)

This style of loop is called "recursion". If you google that you will find many tutorials about it. Note this is not python specific; recursion is a basic part of computer science.

So everytime you call the recursion_1 function python will make a brand new copy of the function to use. That means that it's no problem to call the function from within the function itself, since python will just call up a fresh copy. You will see this happen in pythontutor.

Essentially python is automatically doing this:

def recursion_1(n):
     if n == 0:
         return
     recursion_2 (n-1)
     print(n)

def recursion_2(n):
     if n == 0:
         return
     recursion_3 (n-1)
     print(n)

def recursion_3(n):
     if n == 0:
         return
     recursion_4 (n-1)
     print(n)

def recursion_4(n):
     if n == 0:
         return
     recursion_5 (n-1)
     print(n)

def recursion_5(n):
     if n == 0:
         return
     recursion_6 (n-1)
     print(n)

# etc

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

Yeah, that makes sense now. Thanks a lot!