# 5.3. Instances: A Herd of TurtlesÂ¶

Just like we can have many different integers in a program, we can have many turtles. Each of them is an independent object and we call each one an instance of the Turtle type (class). Each instance has its own attributes and methods â€” so alex might draw with a thin pink pen and be at some position, while tess might be going in her own direction with a fat black pen. So here is what happens when alex completes a square and tess completes her triangle:

Here are some How to think like a computer scientist observations:

• There are 360 degrees in a full circle. If you add up all the turns that a turtle makes, no matter what steps occurred between the turns, you can easily figure out if they add up to some multiple of 360. This should convince you that alex is facing in exactly the same direction as he was when he was first created. (Geometry conventions have 0 degrees facing East and that is the case here too!)

• We could have left out the last turn for alex, but that would not have been as satisfying. If youâ€™re asked to draw a closed shape like a square or a rectangle, it is a good idea to complete all the turns and to leave the turtle back where it started, facing the same direction as it started in. This makes reasoning about the program and composing chunks of code into bigger programs easier for us humans!

• We did the same with tess: she drew her triangle and turned through a full 360 degress. Then we turned her around and moved her aside. Even the blank line 18 is a hint about how the programmerâ€™s mental chunking is working: in big terms, tessâ€™ movements were chunked as â€śdraw the triangleâ€ť (lines 12-17) and then â€śmove away from the originâ€ť (lines 19 and 20).

• One of the key uses for comments is to record your mental chunking, and big ideas. Theyâ€™re not always explicit in the code.

• And, uh-huh, two turtles may not be enough for a herd, but you get the idea!

Mixed up programs

The following program has one turtle, â€śjamalâ€ť, draw a capital L in blue and then another, â€śtinaâ€ť, draw a line to the west in orange as shown to the left:

The program should do all set-up, have â€śjamalâ€ť draw the L, and then have â€śtinaâ€ť draw the line. Finally, it should set the window to close when the user clicks in it.

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

The following program has one turtle, â€śjamalâ€ť, draw a line to the north in blue and then another, â€śtinaâ€ť, draw a line to the east in orange as shown to the left,

The program should import the turtle module, get the window to draw on, create the turtle â€śjamalâ€ť, have it draw a line to the north, then create the turtle â€śtinaâ€ť, and have it draw a line to the east. Finally, it should set the window to close when the user clicks in it.

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

You have attempted of activities on this page