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.

 
1
import turtle            # allows us to use the turtles library
2
turtle.forward(150.0)    # tell alex to move forward by 150 units
3
turtle.left(90.0)        # turn by 90 degrees
4
turtle.forward(75.0)     # complete the second side of a rectangle
5

(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

turtle-2-2: The following program uses a turtle to draw a capital L as shown in the picture to the left of this text, but the lines are mixed up. The program should import the turtle module. Remember that the turtle starts off facing east when it is created. The turtle should turn to face south and draw a line that is 150 pixels long and then turn to face east and draw a line that is 75 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east.

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
turtle-2-3: The following program uses a turtle to draw a checkmark as shown to the left, but the lines are mixed up. The program should import the turtle module. The turtle should turn to face southeast, draw a line that is 75 pixels long, then turn to face northeast, and draw a line that is 150 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east. Northeast is between north and east. Southeast is between south and east.

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)
turtle-2-4: The following program uses a turtle to draw a single line to the west as shown to the left, but the program lines are mixed up. The program should import the turtle module. The turtle should then turn to face west and draw a line that is 75 pixels long.

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.

9
 
1
import turtle
2
3
turtle.color("blue")    # make turtle blue
4
turtle.pensize(3.0)     # set the width of the pen
5
6
turtle.forward(50.0)
7
turtle.left(120.0)
8
turtle.forward(50.0)
9

(ch03_2)

Extend this program …

  1. 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”.)

  2. Do similar changes to allow the user, at runtime, to set tess’ color.

  3. 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 an int. That means you need to convert the string to an int before you pass it to pensize.

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

turtle-2-6: The following program uses a turtle to draw a blue capital L but the lines are mixed up. The program should do all necessary set-up and set the pen size to 10. The turtle should then turn to face south, draw a line that is 150 pixels long, turn to face east, and draw a line that is 75 pixels long.

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)
turtle-2-7: The following program uses a turtle to draw a green capital T, but the lines are mixed up. The program should do all necessary set-up and set the pen size to 10. After that the turtle should turn to face north, draw a line that is 150 pixels long, turn to face west, and draw a line that is 50 pixels long. Next, the turtle should turn 180 degrees and draw a line that is 100 pixels long.

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 turtleturtle.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)
You have attempted 1 of 10 activities on this page
Next Section - 4.3. The for Loop