# NOTE FOR CANOPY USERS: 'turtle' is based on a GUI called 'tkinter' which is different from # what Canopy is based on that's called 'pylab'. Hence, in order to be able to use turtle in # Canopy, you should do the following: # Click on Canopy - Preferences - Python, then uncheck 'Use Pylab'. from turtle import * import time def initturtle(): pencolor('blue') speed('slowest') tracer(1) goto(0,0) pendown() def graphword(str): for char in str: if char == 'F': forward(100) if char == '-': left(90) if char == '[': # when you see a '[', save the current 'state' of the turtle (xpos, ypos) = position() # get current turtle position angle = heading() # get current turtle heading angle TurtleState = (xpos, ypos, angle) # form a tuple containing the state of the turtle stack.append(TurtleState) # store turtle state in a stack if char == ']': # when you see a ']', restore last stored turtle state stackpointer = len(stack) - 1 (xpos, ypos, angle) = stack[stackpointer] # retrieve turtle state from stack del stack[stackpointer] # once retrieved, delete from stack goto(xpos, ypos) # use retrieved info to set the coordinates of the turtle setheading(angle) # also set heading angle initturtle() stack = list() graphword('F[-F]F') done()