Conditions (What If?) 87
w for x in range(number):
x if shape == 'r': # User selected rosette
y t.circle(100)
z else: # Default to polygon
{ t.forward (150)
| t.left(360/number)
At u, we ask the user for a number of sides (for a polygon) or
circles (for a rosette). At v, we give the user a choice between p for
polygon or r for rosette. Run the program a few times, trying each
option with different numbers of sides/circles, and see how the for
loop at w works.
Notice that x through | are indented, so they are part of the
for loop at w and are executed the number of times the user entered
as the number of lines or circles at u. The if statement at x checks
to see if the user entered r to draw a rosette, and if that’s true, y is
executed and draws a circle at this location as part of the rosette. If
the user entered p or anything other than r, the else statement at z
is selected and draws a line at { by default, to make one side of a
polygon. Finally, at | we turn left by the correct number of degrees
(360 degrees divided by the number of sides or rosettes) and keep
looping from w to | until the shape is finished. See Figure 5-5 for
an example.
Figure 5-5: Our PolygonOrRosette.py program with
user input of 7 sides and r for rosette