[Tutor] Recursion

Roger Lea Scherer rls4jc at gmail.com
Mon Jun 18 18:12:11 EDT 2018


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.

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. Maybe: How does
it know to draw the second orange line? and how does it know when to stop
with only two branches from the trunk?

Thank you as always.
-----------------
import turtle

def tree(branchLen, width, t):
    if branchLen > 5:
        t.pensize(width)
        t.forward(branchLen)
        t.right(20)
        tree(branchLen-15, width-5, t)
        t.left(40)
        tree(branchLen-15, width-5, t)
        t.pencolor("orange")
        t.right(20)
        t.backward(branchLen)
        t.pencolor("green")

def main():
    t = turtle.Turtle()
    myWin = turtle.Screen()
    t.speed(0)
    t.left(90)
    t.up()
    t.backward(100)
    t.down()
    t.color("green")
    tree(75, 20, t)
    myWin.exitonclick()

main()

-- 
​
Roger Lea Scherer
623.255.7719

                  *Strengths:*
   Input, Strategic,
​ ​
Responsibility,
​            ​
Learner, Ideation


More information about the Tutor mailing list