Python Forum
recursion function (python beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursion function (python beginner)
#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


Messages In This Thread
recursion function (python beginner) - by everafter - Aug-19-2019, 07:03 AM
RE: recursion function (python beginner) - by buran - Aug-19-2019, 07:05 AM
RE: recursion function (python beginner) - by buran - Aug-19-2019, 07:24 AM

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,791 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Beginner, my recursion returns None, and related problem zpacemanzpiff 2 1,827 Jul-02-2020, 04:25 AM
Last Post: zpacemanzpiff
  Lambda function recursion error DeadlySocks 1 2,086 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  what function should i use to tidy up my code (extreme beginner) scraig0117 4 2,321 Dec-16-2019, 04:03 PM
Last Post: scraig0117
  Beginner problem, replace function with for loop Motley_Cow 9 4,678 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Recursion, with "some_dict={}" function parameter. MvGulik 3 2,475 Aug-02-2019, 01:34 PM
Last Post: MvGulik
  Need help with the while function(Beginner) ogobogo20 1 1,778 May-04-2019, 01:35 AM
Last Post: Larz60+
  function call in recursion moong 2 2,305 Feb-17-2019, 09:48 AM
Last Post: moong
  How recursion function is applied? dukoolsharma 2 2,649 Dec-06-2018, 12:35 PM
Last Post: himanibansal
  Can't understand the output of this recursion function bigmit37 5 3,996 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