🌼 Fun with Flowers

PLTW Computer Science Principles • Unit 1 • Activity 1.1.3 • Mr. Babb

Learn

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.

2 lines of code

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?

Why that is a real problem. Long repetitive programs are miserable to scroll through, they eat storage on the tiny sensors that run real machines, they take forever to write, and every change has to be made hundreds of times. There has to be a better way to write the algorithm — and there is.
Learn

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

College Board connection. On the exam a loop that runs a set number of times is written REPEAT n TIMES { <block of statements> }. Same idea as for with range in Python.
Try it

🔢 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?

Indentation is the loop body. Everything indented under the 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.
Lab

Loop Lab

One loop, three numbers. Move the sliders and watch both the code and the drawing change.

total turn: 360° 0 / 3 solved

The rule to remember. To come back to where you started, the turns have to add up to 360°. So times × degrees = 360.
Build

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.

total turn: 360° 0 / 2 solved

The stem and leaf would be drawn separately — the loop only makes the petals.

How the loop controls this algorithm. The loop variable 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.
Learn

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 = num % 2

rem is 0 when num is even, and 1 when num is odd. Change the divisor and watch the whole pattern change.

try 2, then 3, then 4

Green means remainder 0. Each color is one remainder value — the pattern repeats every 2 numbers.

Is 0 odd or even? 0 ÷ 2 leaves a remainder of 0, so 0 is even. That matters, because the first petal of your flower is petal 0.
Telling time with modulo. It is 1:00 p.m. What time is it 13 hours from now? (1 + 13) % 12 is 2 — 2:00 a.m. Clocks have been doing modulo since long before computers.
Try it

🎨 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.

for petal in range(18): rem = petal % 2 if (rem == 0): painter.color("blue") if (rem == 1): painter.color("darkorchid") painter.circle(50) painter.right(20)
blue petals first
The error everybody makes. == 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?

Challenge

🏢 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.

rem = floor % 2 if (rem > 0): painter.color("blue")
Historical highlight. In 1989 John Henry Thompson created a language called Lingo for Macromedia Director. It let artists control multimedia with code, and it is a direct ancestor of modern game engines, web animation, and graphics.
Exit ticket

Eight questions

—
Conclusion

✍️ Write it out