Python Forum
recursion function (python beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursion function (python beginner)
#1
hi all,

need help for the code below

def tri_recursion(k):
  if(k>0): 
    result = k+tri_recursion(k-1)
    print(result)
  else:
    result = 0
  return result

print("\n\nRecursion Example Results")
tri_recursion(6)
Output:
Recursion Example Results 1 3 6 10 15 21


how does python compute the above code?
how did k becomes greater than 0?
Huh Huh Huh

as per my understanding
if (k>0) then it should jump on else statement
Reply
#2
(Aug-19-2019, 07:03 AM)everafter Wrote: as per my understanding
if (k>0)then it should jump on else statement
exactly the opposite - in this case it will execute lines 3-4

on line 10 you call your function and initial value of k is 6

you can visualise step-by-step execution here
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-19-2019, 07:05 AM)buran Wrote: on line 10 you call your function and initial value of k is 6
oh that make sense to me, I thought tri_recursion(6) is the number times it will recur because I tried these steps, I changed 6 to 9 and the result is:
tri_recursion(6) #result has 6 lines
tri_recursion(9) #result has 9 lines
Reply
#4
(Aug-19-2019, 07:13 AM)everafter Wrote: I thought tri_recursion(6) is the number times it will recur
actually that is also true (in this particular case, because you sum every number, i.e. on line 3 you change k by 1 when call tri_recursion(k-1)) - on line 4 you print intermediate results as well as the final one.

if you change your code
def tri_recursion(k):
  if(k>0): 
    result = k+tri_recursion(k-2) #NOTE THE DIFF HERE
    print(result)
  else:
    result = 0
  return result
 
print("\n\nRecursion Example Results")
tri_recursion(6)
Output:
Recursion Example Results 2 6 12 >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,748 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Beginner, my recursion returns None, and related problem zpacemanzpiff 2 1,800 Jul-02-2020, 04:25 AM
Last Post: zpacemanzpiff
  Lambda function recursion error DeadlySocks 1 2,052 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  what function should i use to tidy up my code (extreme beginner) scraig0117 4 2,290 Dec-16-2019, 04:03 PM
Last Post: scraig0117
  Beginner problem, replace function with for loop Motley_Cow 9 4,615 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Recursion, with "some_dict={}" function parameter. MvGulik 3 2,444 Aug-02-2019, 01:34 PM
Last Post: MvGulik
  Need help with the while function(Beginner) ogobogo20 1 1,756 May-04-2019, 01:35 AM
Last Post: Larz60+
  function call in recursion moong 2 2,278 Feb-17-2019, 09:48 AM
Last Post: moong
  How recursion function is applied? dukoolsharma 2 2,618 Dec-06-2018, 12:35 PM
Last Post: himanibansal
  Can't understand the output of this recursion function bigmit37 5 3,947 Apr-04-2017, 11:15 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020