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

(vip2019) #1

72 Chapter 4


You’ll notice the one drawback to nested loops while you’re
waiting for this program to run: the shapes shown in Figure 4-7
take longer to draw than our simple spirals did. That’s because
we’re performing so many more steps than we did with our
simple spirals. In fact, when we draw the six-sided version of our
ViralSpiral.py, the final drawing consists of 2,352 separate lines!
All those drawing commands, plus the turning and setting the
pen color, add up to a lot of work, even for a fast computer. Nested
loops are useful, but remember that
the extra steps can slow our programs
down, so we use nested loops only
when the effect is worth the wait.
Here’s the completed code for
ViralSpiral.py.

ViralSpiral.py

import turtle
t = turtle.Pen()
t.penup()
turtle.bgcolor("black")
# Ask the user for the number of sides, default to 4, min 2, max 6
sides = int(turtle.numinput("Number of sides",
"How many sides in your spiral of spirals? (2-6)", 4,2,6))
colors = ["red", "yellow", "blue", "green", "purple", "orange"]
# Our outer spiral loop
for m in range(100):
t.forward(m*4)
position = t.position() # Remember this corner of the spiral
heading = t.heading() # Remember the direction we were heading
print(position, heading)
# Our "inner" spiral loop
# Draws a little spiral at each corner of the big spiral
for n in range(int(m/2)):
t.pendown()
t.pencolor(colors[n%sides])
t.forward(2*n)
t.right(360/sides - 2)
t.penup()
t.setx(position[0]) # Go back to the big spiral's x location
t.sety(position[1]) # Go back to the big spiral's y location
t.setheading(heading) # Point in the big spiral's heading
t.left(360/sides + 2) # Aim at the next point on the big spiral
Free download pdf