Drawing Polygons With The Turtle Module
Drawing Polygons With The Turtle Module
1
th E tu rtlE M o du lE
WARNING Do not save the file as turtle.py. This filename already exists and will cause a con-
flict with the import from the turtle module! Anything else will work: myturtle.py,
turtle2.py, mondayturtle.py, and so on.
4 Chapter 1
Moving your turtle
Now that you’ve imported the turtle module, you’re ready to enter instruc-
tions to move the turtle. We’ll use the forward() function (abbreviated as fd)
to move the turtle forward a certain number of steps while leaving a trail
behind it. Note that forward() is one of the functions we just imported from
the turtle module. Enter the following to make the turtle go forward:
forward(100)
Here, we use the forward() function with the number 100 inside paren-
theses to indicate how many steps the turtle should move. In this case, 100
is the argument we pass to the forward() function. All functions take one or
more arguments. Feel free to pass other numbers to this function. When
you press F5 to run the program, a new window should open with an arrow
in the center, as shown in Figure 1-1.
As you can see, the turtle started in the middle of the screen and walked
forward 100 steps (it’s actually 100 pixels). Notice that the default shape is
an arrow, not a turtle, and the default direction the arrow is facing is to the
right. To change the arrow into a turtle, update your code so that it looks
like this:
Changing directions
The turtle can go only in the direction it’s facing. To change the turtle’s
direction, you must first make the turtle turn a specified number of
degrees using the right() or left() function and then go forward. Update
your myturtle.py program by adding the last two lines of code shown next:
Here, we’ll use the right() function (or rt() for short) to make the turtle
turn right 45 degrees before moving forward by 150 steps. When you run this
code, the output should look like Figure 1-3.
6 Chapter 1
As you can see, the turtle started in the middle of the screen, went for-
ward 100 steps, turned right 45 degrees, and then went forward another
150 steps. Notice that Python runs each line of code in order, from top to
bottom.
Here, the range() function creates i, or an iterator, for each for loop. The
iterator is a value that increases each time it’s used. The number 2 in paren-
theses is the argument we pass to the function to control its behavior. This
is similar to the way we passed different values to the forward() and right()
functions in previous sections.
In this case, range(2) creates a sequence of two numbers, 0 and 1. For
each of these two numbers, the for command performs the action specified
after the colon, which is to print the word hello.
Be sure to indent all the lines of the code you want to repeat by pressing
TAB (one tab is four spaces). Indentation tells Python which lines are inside
the loop so for knows exactly what code to repeat. And don’t forget the colon
at the end; it tells the computer what’s coming up after it is in the loop. When
you run the program, you should see the following printed in the shell:
hello
hello