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

(vip2019) #1

12 Chapter 2


Using turtle graphics, not only can you create impressive
visuals with a few lines of code, but you can also follow along with
the turtle and see how each line of code affects its movement. This
will help you understand the logic of your code.

o ur First Turtle program


Let’s write our first program using turtle graphics. Type
the following code into a new window in IDLE and save it
as SquareSpiral1.py. (You can also download this program,
and all the others in the book, at http://www.nostarch.com/
teachkids/.)

SquareSpiral1.py

# SquareSpiral1.py - Draws a square spiral
import turtle
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(90)

When we run this code, we get a pretty neat picture
(Figure 2-1).

Figure 2-1: A hypnotic square spiral, created with
the short program SquareSpiral1.py
Free download pdf