Lab 04: Sixty Bars

Between each pair of heavy hour bars sit four little minute bars. In all,
there are 60 bars: 12 big and 48 small. One loop draws them all, and one
small if decides how big each one is.
Welcome to Lab 04
This is the lab where your dial looks like a real railway clock. You'll
also learn how a program makes a decision. Let's make time tick!
What You Will Learn
- what the remainder operator
%does - how an if / else chooses between two things
- how one loop can draw sixty different things
Run It
Open 04-minute-markers.py in Thonny and click Run. The dial fills
in: every fifth bar is big and bold, and the rest are small.
The Remainder
Dividing 7 by 5 gives 1 with 2 left over. The % operator gives you that
leftover:
1 2 | |
Look at what minute % 5 does as the minute counts up:
| minute | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
minute % 5 |
0 | 1 | 2 | 3 | 4 | 0 | 1 | 2 | 3 | 4 | 0 |
It is 0 exactly on every fifth minute, which is where the big bars go.
Print the Pattern First
Not sure what a loop will do? Add print(minute, minute % 5) inside it
and read Thonny's shell before you draw anything. Seeing the numbers
makes the pattern obvious.
If and Else
1 2 3 4 5 6 | |
Read it out loud: if the remainder is 0, draw a big bar, otherwise draw a
small one. Notice == with two equal signs. One = stores a value
(angle = 6), but two == ask a question: "are these equal?"
The two kinds of bar share an outer edge at 48.5 a. The small one reaches in only to 45, so it is 3.5 a long, and it's 1.4 a wide.
One Equals Sign or Two?
Writing if minute % 5 = 0: with one equals sign is a very common
mistake, and Python answers with a SyntaxError. Fix it by using ==
whenever you're asking whether two things are equal.
Try This
- Make the big bars red by changing their color argument.
- Give the quarter-hour bars (3, 6, 9, and 12) their own color. Put a
new
if minute % 15 == 0:first and turn the oldifintoelif. Why does the order matter? - Change
Aand watch all 60 bars grow or shrink together.
The Dial Is Done!
You used % and if / else to draw two kinds of bar in one loop, and
that's the whole dial. Next, the hands.