
Chapter 6
Selection: If Statements
At both high levels and at the machine level, programs execute statements
one after the other. Selection statements allow a program to execute differ-
ent code depending on what happens as the program runs. This flexibility is
another key to the power of computation.
Listing 6.1: Centipede
1 # centipede.py
2
3 from turtle import
*
4
5 def centipede(length, step, life):
6 penup()
7 theta = 0
8 dtheta = 1
9 for i in range(life):
10 forward(step)
11 left(theta)
12 theta += dtheta
13 stamp()
14 if i > length:
15 clearstamps(1)
16 if theta > 10 or theta < -10:
17 dtheta = -dtheta
18 if ycor() > 350:
19 left(30)
20
21 def main():
22 setworldcoordinates(-400, ...