The first three readings in this course will teach you some fundamental elements of computer programming using something called Turtle Graphics. In these modules, you will be writing Python programs that make a turtle move around the screen.
Some of the elements of programming covered in these modules include:
This module covers five of the commands (or functions) you can use in a Turtle Graphics program. They are: forward, backward, left, right, and color.
If you'd like to follow along, open the following URL in a separate tab or window:
https://repl.it/languages/python_turtle
I would recommend making an account on REPL if you haven't already, so you can save your code and revisit it later.
A Turtle Graphics program, like any program, can be thought of as a sequence of instructions. For now, each instruction has some measurable result, and the instructions are executed strictly in order from the first instruction to the last instruction.
When you write a Turtle Graphics program, your instructions are given to a turtle object (which will show up as a little arrow on your screen). When executed together in order, your instructions will cause the turtle object to move around the screen and draw lines, like this:
Whenever you start a new Turtle Graphics program, make sure to put the following line of code at the top of your program:
from turtle import *
Don't worry about what this means just yet. Just remember that for this class, you should always have it as the first line of your program.
The first two commands you are going to use are forward and backward. As you can imagine, they make your turtle object move forward and backward on the screen.
To invoke the forward command, add this line to your program:
forward(50)
Since you also need the import line mentioned before, your full program should look like this:
from turtle import *
forward(50)
Now, when you click the run button near the top of your screen, you should notice an arrow move across the right side of your screen. Once it is done moving, your screen should look something like this:
Notice that the path taken by the arrow is traced with a black line. Later, we'll learn how to change the color of the lines drawn by the arrow.
The backward command is similar. Here is a program that only uses the backward command:
from turtle import *
backward(50)
As you probably predicted, this moves the arrow in the opposite direction:
You can write a program with multiple commands. For now, they will be executed in order from top to bottom. Here's a program that uses both the forward command and the backward command:
from turtle import *
forward(50)
backward(25)
Notice the way the forward and backward commands are written. They are spelled in all lower case and immediately followed by a pair of parentheses surrounding a number. This is more or less how the commands have to be written. If you change one small thing - by making the first letter capitalized or forgetting the parentheses, for example - your program will not be understood.
Try changing small things about how the commands are written. Notice what happens when you do so. Does the program run to completion? Does any of it run?
If you write a command that Python does not understand, you can see what Python has to say about it by switching from the "result" tab to the "console" tab on the right side of your screen, like this:
You can change the direction of the turtle using the left and right commands. These commands are similar to the forward and backward commands in how they are invoked. One important difference is that the number in parentheses represents a number of degrees rather than a number of pixels.
Watch what happens when you turn the turtle to the left by 90 degrees and then send the turtle forward 50 pixels:
from turtle import *
left(90)
forward(50)
Now, watch what happens if you turn the turtle to the right instead of to the left:
from turtle import *
right(90)
forward(50)
Let's get colorful! This is where you can really start to let out your inner artist.
To change the turtle's color, you can use the color command. The color command is similar to the commands we've learned so far in a couple ways:
The most important difference is that you do not put a number in the parentheses after the word color. Instead, you put a string - a unit of textual information surrounded by quotes. This string is the name of the color you would like the turtle to have at a particular moment in your program.
You can surround the color with single quotes, like this:
color('red')
You can also use double quotes, like this:
color("red")
In this class, we will be using single quotes by convention.
Here's an example program that uses the color command:
from turtle import *
color('red')
forward(50)
Here are a couple example programs that put all the concepts in this module together.
from turtle import *
color('blue')
forward(50)
left(90)
color('green')
forward(50)
left(90)
from turtle import * color('red') forward(20) color('yellow') forward(20) color('green') forward(20) color('blue') forward(20) color('purple') forward(20)
1. Which of the following is the correct way to move the arrow forward by 30 in a Turtle Graphics program?
a.forward(30)b.
30 forwardc.
forward 30d.
30(forward)
2. What does the following program draw?
from turtle import * forward(20) color('red') forward(20)a.
It draws a line segment that is red.
b.It draws two perpendicular lines, one of which is red and one of which is black.
c.It draws a line segment that is half red and half black.
d.It draws two perpendicular lines that are red.
For each of these exercises, you should start by creating a new Turtle Graphics REPL session. You can do this by going to:
https://repl.it/languages/python_turtle
This Turtle Graphics program attempts to draw a single black line of length 50. However, something isn't quite right about it. Copy the code into a new Turtle Graphics REPL session and see if you can fix it!
forward(50)
When the program is fixed, it should draw this:
This Turtle Graphics program attempts to draw a line segment that alternates black, green, black, green. Currently, it does not do this. Copy the code into a new Turtle Graphics REPL session and see if you can fix it!
from turtle import *
forward(20)
color('green')
forward(20)
forward(20)
color('green')
forward(20)
When the program is fixed, it should draw this:
Open a new Turtle Graphics REPL session and write a program that draws two distinct vertical line segments - one green segment of length 40 going downwards from the origin, and one yellow segment of length 40 going upwards from the origin.
If you draw a line segment of length 80 and cover half of it with another line segment, that does not count as a line segment of length 40! Similarly, if you draw multiple smaller line segments whose lengths add up to 40, that also does not count as a line segment of length 40.
When your program is finished, it should look like this:
Note that the arrow started at the middle of the two line segments. Your arrow's final location may be different depending on the order in which you draw the line segments.
Open a new Turtle Graphics REPL session and write a program that draws the following:
Recall from the last exercise that you should not do the following:
Here is an example of what your result might look like: