Repository navigation
Expand file tree
/
Copy path01_while_loop.py
More file actions
49 lines (39 loc) · 1.72 KB
/
Copy path01_while_loop.py
File metadata and controls
49 lines (39 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# ============================================================
# CONCEPT: the while loop
# ============================================================
# A while loop repeats a block AS LONG AS a condition is True.
# The three things to watch (these are what a debugger shows you):
# 1. the loop variable -> 'side'
# 2. how it CHANGES each pass -> side += step
# 3. the CONDITION it is tested against -> side < MAX_SIDE
#
# If the variable never moves toward making the condition False,
# the loop runs forever. That is the #1 bug to teach.
# ============================================================
import turtle
MAX_SIDE = 200 # stop once the square's side reaches this
side = 20 # the loop variable (start small)
step = 12 # how much it grows each pass
angle = 91 # 91 (not 90) makes the squares spiral nicely
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("while loop: keep drawing WHILE side < MAX")
pen = turtle.Turtle()
pen.color("cyan")
pen.speed(3) # slow enough to WATCH the loop happen
iteration = 0
# ---- the loop -------------------------------------------------
while side < MAX_SIDE:
iteration += 1
# TRACE: this printout mirrors a debugger 'watch' window.
keep_going = side < MAX_SIDE
print(f"pass {iteration:2d} | side = {side:3d} | side < {MAX_SIDE}? {keep_going} -> draw")
# draw one square
for _ in range(4):
pen.forward(side)
pen.right(angle)
side += step # <-- THE STEP. Remove this line => infinite loop!
# ---- after the loop -------------------------------------------
print(f"\nLOOP ENDED: side = {side}, which is NOT < {MAX_SIDE}. Condition is now False.")
pen.hideturtle()
screen.exitonclick()