23  Turtle: the object

import steps_widget

Now you know that all Python values are objects, here is a hard one with four legs. The turtle object is a critter that you can steer around a canvas to make it draw lines and patterns with its tail. It is made with visual learners in mind turning code patterns into actual patterns.

Exercise 23-1

SOLO

Run the code below to see what happens.

from turtle_widget import Turtle

t = Turtle()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)

The left and right methods turn the turtle left or right by the specified number of degrees. Use the forward method to make the turtle move forward some number of steps the way it is already facing.

Commands and questions

The turtle methods can be divided into to kinds: The first are the ones that make the turtle do something, thereby changing the state of the turtle object in terms of location, direction, etc. (like forward, backward, left, right, penup, pendown and goto). The second are are methods for extracting information about the state of the turtle object: where it is, which way it points etc. (like position, xcor, ycor, heading, isdown and isvisible).

Exercise 23-2

SOLO

What do the different turtle methods return? Run the two cells below to see if you can figure it out.

from turtle_widget import Turtle

t = Turtle()
t.forward(80)
t.left(120)
t.forward(80)
t.left(120)
t.forward(80)
from turtle_widget import Turtle

t = Turtle()
t.forward(80).left(120).forward(80).left(120).forward(80)

Where is my turtle

The turtle starts out in the middle of the canvas facing right. Every position on the canvas as an x,y coordinate with x values growing to the right and y values growing upwards. A heading of zero means the turtle is facing right and the heading grows as the turtle turns counter-clockwise. So a heading of 90 is straight up.

Exercise 23-3

SOLO

Decide what is printed below before you run the cell.

from turtle_widget import Turtle

t = Turtle()
t.forward(100)
t.left(90)
t.forward(50)
print(t.xcor())
print(t.ycor())
print(t.heading())

Did you get it right? If not, figure out what your mistake was.

The method position method returns both coordinates at once, packed into a tuple:

from turtle_widget import Turtle

t = Turtle()
t.forward(120)
t.left(90)
t.forward(60)
here = t.position()
print(here)
x, y = t.position()
print(y)

The second-to-last line unpacks the tuple assigning the values to two variables in one go.

Exercise 23-4

SOLO

The pen (tied to the turtle’s tail) is also part of turtle’s state just like its position. You can control it and ask about it like this:

from turtle_widget import Turtle

t = Turtle()
print(t.isdown())
t.forward(80)
t.penup()
print(t.isdown())
t.forward(80)
t.pendown()
t.forward(80)
print(t.isdown())

Look at the picture and the numbers. Why is there a gap in the line?

Two names, same turtle

This week you saw that giving a list a second name does not give you a second list. Turtles behave the same way, and because a turtle is visible the demonstration is unusually blunt.

Exercise 23-5

SOLO

Decide what the drawing will look like and what the last code line prints. Then run it.

from turtle_widget import Turtle

t = Turtle()
other = t
other.forward(100)
other.left(90)
other.forward(100)
print(t.position())

Everything you did using other happened to t, because other and t are the same thing. It works the same as it does with a variable pointing to a list: a = [1, 2, 3] followed by b = a makes b point to the same list as a. t points to an object of type Turtle so other = t makes other point to whatever t points to.

Drawing with data

A drawing is data before it is a picture, and this section is where you start writing the data down. You do not yet have loops, so you will index into your containers by hand, which is a perfectly good way to see what is in them.

Exercise 23-6

SOLO

Fill in the missing code to make the turtle walk the four distances in the list, each separated by a 90 degree left turn. Use indexing to extract each distance from the list.

from turtle_widget import Turtle

distances = [120, 60, 120, 60]

t = Turtle()
t.forward(distances[0])
t.left(90)

Exercise 23-7

SOLO

A dictionary is the natural way to pair DNA bases with colors. Predict which three colors appear, in which order, then run it.

from turtle_widget import Turtle

color = {'C': 'orange', 'T': 'blue', 'G': 'green', 'A': 'red'}
dna = 'GAT'

t = Turtle()
t.pensize(6)
t.pencolor(color[dna[0]])
t.forward(80)
t.pencolor(color[dna[1]])
t.forward(80)
t.pencolor(color[dna[2]])
t.forward(80)

On. paper, do the steps reducing color[dna[0]] to a single value. Then see if you got it right.

%%steps
color[dna[0]] # print steps