4.2. Our First Turtle Program¶
Let’s try a couple of lines of Python code to draw a simple figure like a rectangle.
The program as shown will only draw the first two sides of the rectangle. After line 2 you will have a straight line going from the center of the drawing canvas towards the right. After line 4, you will have a canvas with a turtle and a half drawn rectangle. Press the run button to try it and see.
import turtle # allows us to use the turtles library
turtle.forward(150.0) # tell alex to move forward by 150 units
turtle.left(90.0) # turn by 90 degrees
turtle.forward(75.0) # complete the second side of a rectangle
(ch03_1)
Here are a couple of things you’ll need to understand about this program.
The first line tells Python to load a module named turtle
.
That module allows you to use code someone else has written to create
drawings. The import line must come before all other lines. With the
first line set up, we are ready to do some drawing.
In lines 2-4, we instruct the turtle to move and to turn. We do this
by invoking or activating the turtle functions — these are
the instructions that all turtles know how to respond to. Here the
dot indicates that the functions invoked belong to and refer to the
turtle
module.
Complete the rectangle …
Modify the program by adding the commands necessary to have the turtle complete the rectangle.
Check your understanding
turtle-2-1: Which direction does the Turtle face when it is created?
Mixed up programs

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.
turtle.left(90.0)
turtle.forward(75.0)
turtle.right(90.0)
turtle.forward(150.0)
import turtle

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.
import turtle
turtle.left(90.0)
turtle.forward(150.0)
turtle.right(45.0)
turtle.forward(75.0)

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.
turtle.left(180.0)
import turtle
turtle.forward(75.0)
Turtles also have attributes — (sometimes called properties). For example, the turtle has a color attribute. The function invocation turtle.color(“red”) will make the turtle red and the line that it draws will be red too.
The color of the turtle, the width of its pen(tail), the position of the turtle within the window, which way it is facing, and so on are all part of its current state.
Quite a number of methods exist that allow us to modify the turtle. In the example below, we show just show a couple and have only commented those lines that are different from the previous example.
import turtle
turtle.color("blue") # make turtle blue
turtle.pensize(3.0) # set the width of the pen
turtle.forward(50.0)
turtle.left(120.0)
turtle.forward(50.0)
(ch03_2)
Extend this program …
Modify this program so that it prompts the user to enter the desired pen color. It should store the user’s responses in a variable, and modify the color of the window according to the user’s wishes. (Hint: you can find a list of permitted color names at https://www.w3schools.com/colors/colors_names.asp. It includes some quite unusual ones, like “PeachPuff” and “HotPink”.)
Do similar changes to allow the user, at runtime, to set tess’ color.
Do the same for the width of tess’ pen. Hint: your dialog with the user will return a string, but tess’
pensize
method expects its argument to be anint
. That means you need to convert the string to an int before you pass it topensize
.
Check your understanding
turtle-2-5: Consider the following code:
import turtle
turtle.forward(150.0)
turtle.left(90.0)
turtle.forward(75.0)
What does the line “import turtle” do?
Mixed up programs
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.
turtle.left(90.0)
turtle.forward(75.0)
import turtle
turtle.color("blue")
turtle.pensize(10.0)
turtle.right(90.0)
turtle.forward(150.0)
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.
import turtle
turtle.color("white")
turtle.pensize(10.0)
turtle.right(180.0)
turtle.forward(100.0)
turtle.left(90.0)
turtle.forward(150.0)
turtle.left(90.0)
turtle.forward(50.0)