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

(vip2019) #1

24 Chapter 2


Always willing to help make my programs more awesome, Alex
asked for one more change: what if we replaced the line segments
with circles now? Wouldn’t that be the coolest picture of all? Well,
yes, I have to agree—it is even cooler. Here’s the full code.

ColorCircleSpiral.py

import turtle
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]
for x in range(100):
t.pencolor(colors[x%4])
t.circle(x)
t.left(91)

You can see the result in Figure 2 -7.

Figure 2-7: Alex’s awesome color circle spiral—
eight lines of code, simple and elegant

http://www.allitebooks.com
Free download pdf