Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Python) Trying to turn this tree drawing program into a recusive tree drawing p

ID: 3938649 • Letter: #

Question

(Python) Trying to turn this tree drawing program into a recusive tree drawing program. Make two recursive calls in drawtree with smaller length and width. Let the same program deal with how the smaller trees are drawn.

import turtle

def drawtree(length, w, t):
   def drawbranch(length, w, t):
       t.width(w)
       t.forward(length)

   drawbranch(length, w, t)
   t.left(45)
   drawbranch(length * 0.6, w * 0.6, t)
   t.backward(length * 0.6)
   t.right(90)
   drawbranch(length * 0.6, w * 0.6, t)
   t.backward(length * 0.6)
   t.left(45)
   t.backward(length)

def main():
   t = turtle.Turtle()
   screen = t.getscreen()
   t.goto(0, -200)
   t.speed(100)
   t.seth(90)
   drawtree(200, 20, t)
   screen.exitonclick()

Explanation / Answer

import turtle

myTurtle = turtle.Turtle()
myWin = turtle.Screen()

def drawSpiral(myTurtle, lineLen):
    if lineLen > 0:
        myTurtle.forward(lineLen)
        myTurtle.right(90)
        drawSpiral(myTurtle,lineLen-5)

drawSpiral(myTurtle,100)
myWin.exitonclick()