Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Turtle Graphics: Drawing with Python 17

This is also a nice visual to help
you understand how being off by just
one number can drastically change the
result of your program. One degree
doesn’t seem like a big deal, unless
you’re off by one degree 100 times
(which adds up to 100 degrees), or
1,000 times, or if you’re using a pro-
gram to land an airplane...
If you don’t know how degrees work yet, don’t worry about
it for now. Just play with the numbers and see what happens.
Change the number of lines the program draws to 200, or 500,
or 50, by changing the value in parentheses after range.
Also try changing the angle in the last line to 91, 46, 61, or
121, and so on. Remember to save the program each time. Then
run it to see how your changes affect what the program draws.
Older readers who know a bit of geometry will see some familiar
shapes based on the angles used and may even be able to predict
the shape based on the angle before the program runs. Younger
readers can just enjoy changing things up a bit, and this exer-
cise might come back to them when they’re in a geometry class
someday.

Turtle roundup


Speaking of geometry, turtle graphics can draw lots more interest-
ing shapes than just straight lines. We’ll come back to the square
shape again in the next section, but let’s take a short detour to
check out more of the Python Turtle library.
Let’s change one more line of code: t.forward(x). We saw earlier
that this command, or function, moves the turtle’s pen forward
x pixels and draws a straight line segment; then the turtle turns
and does it again. What if we changed that line of code to draw
something more complex, like a circle?
Fortunately for us, the command to draw a circle of a certain
size, or radius, is as simple to code as the command to draw a
straight line. Change t.forward(x) to t.circle(x), as shown in the
following code.
Free download pdf