Turtles Drawing
Turtles Drawing
if it is not already launched. Start a NEW file (Click icon .). The file will
contain python commands. It will be referred to as the “python program file”.
b. Type the following commands below and click on the green arrow at the
top to execute the code. Record down what you see. Save your python
program file to a folder (Click icon ).
c. Instead of 2 commands, consider that there are now four commands that you
can use: forward(x), left(x), penup(), pendown().
Command Action
forward(x) Instruct the turtle to move x steps in the direction it is facing
left(x) Instruct the turtle to make a x degrees left turn (instead of
just 90 degree)
penup() Instruct the turtle to lift up the pen (i.e. stop drawing)
pendown() Instruct the turtle to put down the pen (i.e. start drawing)
1
d. Take note of the following when writing your turtle commands:
i. The first 2 lines of commands issued must be as follows. You do not
have to worry about what these 2 lines mean at the moment.
e. Write the sequence of commands to instruct the turtle to draw the picture as
shown in figure 3 below. Try to do it with the least number of commands.
(Note: We often refer to a collection of commands as “program code” or just
“code”.)
Figure 3 Step
2
2. Debugging your program
a. A software bug (or “bug” in short) is an error, flaw, failure, or fault in a computer
program or system that produces an incorrect or unexpected result, or causes it to
behave in unintended ways. The process of removing the bug from your program is
called “debugging”.
b. When you write code you might encounter some errors due to a bug. For example
you might have mistyped the forward() command as follows:
c. In such a case, when you execute the code, the program stops executing when it
encounters a bug like this and you will see an error similar to the following
appearing in the “python shell” window of the IDE. It is important not to panic and
read the error message carefully.
d. The 2 most important information that you must pay attention to is the line
number and the last line that inform the programmer (you) about the type of
error. In this case, the error happens at line 9 of the program file and the type of
error is a “NameError”. It informs you that the computer does not understand
forwad.
You will encounter many other types of errors at this module. The important thing
to remember is not to worry. There are reasons why errors occur and they can
be corrected.
What are other kinds of errors that you have encountered? Talk to your facilitator
about it.
3
3. What is the turtle drawing?
a. In the previous exercise, you gave the turtle a sequence of commands to draw 2
pictures. In this exercise, you will step into the shoes of the turtle and try to
predict what drawing will be produced by studying the sequence of commands.
Read the code given in figure below and produce the corresponding drawings based
on the commands given.
4
4. More Turtle Commands
a. You can change the color of the line drawn by the turtle using the pencolor(x)
command. Type out the commands below in a new program file, execute it and
note down what you observed. (NOTE: the value of x can be ‘red’, ‘blue’, ‘yellow’,
‘purple’ etc. The word must be enclosed in quotes.)
b. Modify the code such that the turtle is able to draw a ‘blue’ square of length 200
steps with the turtle starting and ending at the same location facing the
right.
5
4.2 Moving to a Desired Location Faster!
a. Type out the code below in a new python file and execute it. Experiment with
different values for the goto(x,y) command and answer the questions that
follow.
ii. How many values does the goto(x, y) command take in? What do the
values represent?
b. Write the code to draw the figure below. Make use of the goto(x,y) command to
position the turtle.
6
50
50
40
40
40
80
Note: You can make the turtle make a left turn of 300 by issuing the following
command: left(30)