Today’s mission 5 min
Make the hub count, remember and react to its buttons using Python loops and variables.
You already use repeat, forever and variables in blocks. Today you learn how Python writes each one. By the end you will build a button-controlled score counter from a blank file.
Why it matters for WRO. Every competition program is loops and variables: follow until the line, count the objects, remember which colour came first. Get these right now, on a hub that cannot drive off the table.
The idea — indentation is the C-shape 10 min
A repeat block wraps other blocks inside its C-shape. Python has no C-shape. Instead, the lines inside the loop are pushed to the right by four spaces. That push is called indentation, and Python takes it seriously.
for i in range(5):is repeat 5. The colon at the end says “the loop starts here”.while True:is forever.while count < 10:is repeat until count = 10, turned the other way round: Python says how long to keep going, not when to stop.count = 0is set count to 0.count = count + 1is change count by 1.
A variable needs no block to create it. The first time you write count = 0, the variable exists. Use names that say what they hold: score, not x.
Blocks vs Python 15 min
Two programs, each shown both ways. Read the first one line by line.
SPIKE word blocks
when program starts :: events hat set [count v] to (0) :: variables repeat (5) change [count v] by (1) :: variables write (count :: variables) :: display play beep (69) for (0.1) seconds :: sound wait (0.5) seconds end
Pybricks Python
from pybricks.hubs import PrimeHub from pybricks.tools import wait hub = PrimeHub() count = 0 for i in range(5): count = count + 1 hub.display.number(count) hub.speaker.beep(440, 100) wait(500) print("count is", count)
| Block | Python | Watch out |
|---|---|---|
| set count to 0 | count = 0 | One equals sign means "store". |
| repeat 5 | for i in range(5): | i counts 0, 1, 2, 3, 4 — it starts at 0, not 1. |
| (blocks inside the C) | (lines indented 4 spaces) | Wrong indentation = IndentationError, or a line outside the loop. |
| change count by 1 | count = count + 1 | count += 1 does the same. |
| play beep 69 for 0.1 seconds | hub.speaker.beep(440, 100) | Note number vs hertz; seconds vs milliseconds. |
| wait 0.5 seconds | wait(500) | Milliseconds again. |
SPIKE word blocks
when program starts :: events hat set [score v] to (0) :: variables forever if <is [left v] button [pressed v]? :: sensors> then change [score v] by (-1) :: variables end if <is [right v] button [pressed v]? :: sensors> then change [score v] by (1) :: variables end write (score :: variables) :: display wait (0.15) seconds end
Pybricks Python
from pybricks.hubs import PrimeHub from pybricks.parameters import Button from pybricks.tools import wait hub = PrimeHub() score = 0 while True: pressed = hub.buttons.pressed() if Button.LEFT in pressed: score = score - 1 if Button.RIGHT in pressed: score = score + 1 hub.display.number(score) wait(150)
| Block | Python | Watch out |
|---|---|---|
| forever | while True: | Capital T. true is a NameError. |
| is left button pressed? | Button.LEFT in pressed | pressed() gives the set of buttons held right now. |
| (centre button) | (stops the program) | The centre button is the stop button in Pybricks. |
Why read the buttons into pressed once, then test it twice? So both tests look at the same moment. It is a habit that matters later, when sensors change between two readings.
Write it from scratch 20 min
New file: loops. Type every line. Run after each step.
- Print the loop counter. Write a
for i in range(5):loop whose only job isprint(i). Look at the terminal. Which numbers appeared? Change it torange(1, 6)and run again. - Count with a variable. Add
count = 0before the loop andcount = count + 1inside it. Show it on the display. - Count down instead. Start at 10 and use a
whileloop that keeps going whilecount > 0. - The button score. Type the second program above from memory, then check it against the page.
A while loop that counts down
count = 10 while count > 0: hub.display.number(count) wait(300) count = count - 1 hub.display.text("END")
Pybricks Code adds the indentation for you after a line ending in a colon. Press Backspace once to come back out of the loop. That one key decides which lines repeat.
Test and measure 15 min
- In the count-down, move
count = count - 1out of the loop (Backspace once). Run it. What does the hub do? Stop it with the centre button. You have made an endless loop — the condition can never become false. - In the button score, change
wait(150)towait(10). Press a button once. How far does the score jump? Why? - Hold the right button until the score reaches 99, then once more. What does the display show? Write the limit in your journal.
- Add
print(score)inside the loop. How many lines appear in the terminal each second?
Break it, fix it 10 min
One mistake in each. Run it, read the last line of the error, fix it alone.
Bug 1
from pybricks.hubs import PrimeHub
from pybricks.tools import wait
hub = PrimeHub()
for i in range(5)
hub.display.number(i)
wait(500)Bug 2
from pybricks.hubs import PrimeHub from pybricks.tools import wait hub = PrimeHub() for i in range(5): hub.display.number(i) wait(500)
Bug 3
from pybricks.hubs import PrimeHub
from pybricks.tools import wait
hub = PrimeHub()
for i in range(5):
total = total + i
wait(100)
print(total)Three kinds of error to know by name. SyntaxError: Python cannot read the line — often a missing colon. IndentationError: the spaces are wrong. NameError: a variable is used before it was given a value.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page. You may use your own earlier programs and the Pybricks documentation, nothing else.
Safe counter. Write a new program, counter, that:
- starts at 0 and shows the number on the display;
- adds 1 for the right button and takes 1 away for the left;
- never goes below 0 and never above 20;
- beeps high at 20 and low at 0, when you try to go past them;
- turns the light green on even numbers and red on odd ones.
Hint you may use: score % 2 == 0 is true when score is even.
Done means: another team presses the buttons however they like and cannot make it break.
Engineering journal 5 min
Today’s entry:
- the Python for repeat, forever and repeat until
- what
range(5)really counts - the three error names, and one you met today
- the largest number the display can show
- how you stopped the counter going past 0 and 20