You already used circle(). How is it written?
Last activity you called painter.circle(100) and a circle appeared. But something
had to draw it. A turtle can only go straight and turn — so a circle is really a polygon with so
many tiny sides that your eye gives up and calls it round.
Move forward 20 pixels, turn right 20 degrees, and repeat. Watch what you get.
An arc so far.
🧠 Quick check
You want a smoother circle, so you use the same two methods but move 1 pixel and turn 1 degree at a time. Written out the long way, how many lines of code is that?
Say it once. Run it many times.
Running a block of statements several times is called iteration. An iterative block changes the straight-down flow of your program by repeating a set of statements until a stopping condition is met.
Click each part of the loop header to find out what it does. 0 / 5 found
REPEAT n TIMES { <block of statements> }. Same idea as
for with range in Python.
🔢 What does range() actually give you?
range(start, stop, step). start is optional and defaults to 0.
step is optional and defaults to 1. stop is required — and the stop number is
not included.
The crossed-out number is stop. It tells the loop where to quit, and never gets used.
🧠 Quick check
Which call produces exactly 2 4 6 8?
for line runs
every time around. The moment a line goes back to the left margin, the loop is over.
PLTW uses two spaces per level to fit more code on screen; VS Code defaults to four. Pick one and stay
with it — mixing them is an IndentationError.
Loop Lab
One loop, three numbers. Move the sliders and watch both the code and the drawing change.
times × degrees = 360.
A circle of circles
Now put a whole shape inside the loop instead of one line. Draw a circle, turn a little, draw another circle — all the way around. That is a flower.
The stem and leaf would be drawn separately — the loop only makes the petals.
petal starts at 0,
the first value in the list from range(18). Each time the body runs and draws a
circle, petal takes the next value. Once it has taken them all, the loop ends.
The modulo operator: %
What if you want every other petal a different color? Your program has to work out whether the petal number
is odd or even. In Python, % gives you the remainder of a division.
rem is 0 when num is even, and 1 when num is odd. Change the
divisor and watch the whole pattern change.
Green means remainder 0. Each color is one remainder value — the pattern repeats every 2 numbers.
(1 + 13) % 12 is 2 — 2:00 a.m. Clocks have been doing modulo since
long before computers.
🎨 Alternating petals
This is the flower loop with an if that checks the remainder. Flip the number it
compares to and watch which petals change.
== asks "are these equal?"
= says "store this value here." Write
if (rem = 0): and Python stops with a SyntaxError.
🧠 Quick check
You want every fourth petal a new color. Which test do you use?
🏢 A tower of modulus
An architect wants a tower whose walls change color every three floors: floors 0, 1, 2 gray, floors 3, 4, 5 blue, floors 6, 7, 8 gray, and so on. One line of modulo does the whole thing.