#import Python turtle module #More info see https://docs.python.org/2/library/turtle.html import turtle base = 1 #set the length for base case reduction = 6 #length decreased def drawSpiral(myTurtle, lineLen): if lineLen <= base: # base case myTurtle.forward(base) else: #Move the turtle forward by lineLen in the direction the turtle is headed. myTurtle.forward(lineLen) #Turn turtle right by 90 degrees. myTurtle.right(90) #Recursively do this by decreasing the lineLen drawSpiral(myTurtle,lineLen - reduction) ##Initialize the turtle t = turtle.Turtle() myWin = turtle.Screen() t.speed(6) #Set the turtle's speed. 1 is slow and 10 is fast. t.color("blue") #Set the turtle's color. t.pensize(2) #Set the pen size ##Set the value for size size = 200 t.up() #Pull the pen up - no drawing when moving. t.setposition(-size/2,size/2) #move the turtle to the starting point t.down() #Pull the pen down - drawing when moving. ##draw the spiral drawSpiral(t,size) ##exit the drawing screen myWin.exitonclick()
Program Revised from http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsintro-VisualizingRecursion.html