[Tutor] Recursion

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jun 18 20:13:12 EDT 2018


On 18/06/18 23:12, Roger Lea Scherer wrote:
> My foggy understanding of recursion is probably the reason I can't figure
> this out. When turtle draws this program there is an orange line in the
> green which I would prefer not to have. I've tried all I could think of,
> but can't get the orange line to go away, or maybe more accurately, not to
> be drawn.

Remove the line in tree() that sets the color to orange?

> The program goes to the end of the recursion and then starts drawing? which
> seems wrong, because the trunk of the tree is drawn first.

You call the recursing function before and after drawing
so it draws on the last call and on the way back up to
the first call, like this:

tree
   draw
   tree
     draw
     tree
        draw
        tree
        draw
     draw
   draw
draw

> it know to draw the second orange line? 

I'm not sure what you mean by second but you draw orange
in every call to tree.

> and how does it know when to stop
> with only two branches from the trunk?

Again I'm not sure what's happening because I'm not
running the code.

> def tree(branchLen, width, t):
>     if branchLen > 5:
>         t.pensize(width)
>         t.forward(branchLen)

This should draw in green

>         t.right(20)
>         tree(branchLen-15, width-5, t)

this draws in green and orange

>         t.left(40)
>         tree(branchLen-15, width-5, t)

this draws green and orange

>         t.pencolor("orange")
>         t.right(20)
>         t.backward(branchLen)

this draws orange

>         t.pencolor("green")


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list