Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursive action
#1
i dont understand how this code works, as per my understanding every time the def function will be called , why it returns a result.

def rabbits(n):
    if n<1:
        return 0
    else:
        if n==0 or n==1:
            return 1
        else:
            return rabbits(n-1)+rabbits(n-2)-rabbits(n-5)
Reply
#2
For this, I recommend you get a whiteboard or piece of paper, and draw it out as a tree. Start at the top with the first call. Then write below that any recursive calls. And write below those other recursive calls. When you write them out, I recommend doing it as "rabbits(whatever number)".
Reply
#3
(Jun-06-2017, 01:01 PM)elhetch Wrote: i dont understand how this code works, as per my understanding every time the def function will be called , why it returns a result.

def rabbits(n):
    if n<1:
        return 0
    else:
        if n==0 or n==1:
            return 1
        else:
            return rabbits(n-1)+rabbits(n-2)-rabbits(n-5)

    if n<1:
overrides
if n == 0
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
(Jun-06-2017, 03:46 PM)micseydel Wrote: I recommend you get a whiteboard or piece of paper, and draw it out as a tree. Start at the top with the first call. Then write below that any recursive calls. And write below those other recursive calls.
Or download Thonny
it's has the best way to display recursive calls that i have seen.
When step into debugger,it will show a new window for each recursive call with all values that changing.
Reply
#5
(Jun-06-2017, 04:03 PM)snippsat Wrote: Or
I might recommend that for experienced programmers, but struggling aids learning.
Reply
#6
(Jun-06-2017, 04:04 PM)micseydel Wrote: I might recommend that for experienced programmers, but struggling aids learning.
Thonny is designed for total beginners,it's really great i use Thonny sometime myself.
I can agree whiteboard or piece of paper is good way to learn without using any tool.
Reply
#7
I don't want to hijack this thread, but I would suggest the OP try to do it manually, and definitely use Thonny if any of these are true: they've mastered walking through code and want it automated, they have tried it manually and struggled and need further help, they have successfully done it manually but want to verify it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pixel color and action Sartre 4 2,068 Apr-13-2023, 03:26 AM
Last Post: Sartre
Question Running an action only between certain times alexbca 9 1,718 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Checkbox itens with a button to run action Woogmoog 3 945 Dec-19-2022, 11:54 AM
Last Post: Woogmoog
Question Running an action only if time condition is met alexbca 5 1,308 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,356 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Have an amount of time to perform and action CookieGamez2018 1 2,944 Dec-21-2018, 07:12 AM
Last Post: Gribouillis
  action on MQTT while long loop is running runboy 4 6,070 Oct-05-2018, 11:57 PM
Last Post: runboy
  Hourly action resetter with threading? alex36540 3 3,324 Jan-10-2018, 07:13 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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