Conditions (What If?) 89
Ask the user for the number of sides, default to 4
sides = int(turtle.numinput("Number of sides",
"How many sides in your spiral?", 4))
Our outer spiral loop for polygons and rosettes, from size 5 to 75
u for m in range(5,75):
t.left(360/sides + 5)
v t.width(m//25+1)
w t.penup() # Don't draw lines on spiral
t.forward(m*4) # Move to next corner
x t.pendown() # Get ready to draw
Draw a little rosette at each EVEN corner of the spiral
y if (m % 2 == 0):
z for n in range(sides):
t.circle(m/3)
t.right(360/sides)
OR, draw a little polygon at each ODD corner of the spiral
{ else:
| for n in range(sides):
t.forward(m)
t.right(360/sides)
Let’s look at how this program works. At u, we set up a for
loop over the range 5 to 75; we’re skipping 0 to 4 because it’s hard
to see shapes that are 4 pixels across or smaller. We turn for our
spiral; then, at v we use integer division to make the pen wider
(thicker) after every 25th shape. Figure 5-6 shows the lines getting
thicker as the shapes get bigger.
At w, we lift our turtle’s pen off the screen and move forward
so we don’t draw lines between rosettes and polygons. At x, we put
the pen back down and get ready to draw a shape at the corner
of the big spiral. At y, we test our loop variable m to see if we’re
drawing at an even corner. If m is even (m % 2 == 0), we draw the
rosette with the for loop at z. Otherwise, the else at { tells us to
draw a polygon using the for loop beginning at |.