Today’s mission 5 min
Drive past four objects in a random order, remember every colour, then plan where each one goes.
At WRO some objects are placed at random before each round. You do not know the order until the robot looks. A program with a fixed route works one round in many. A program that remembers what it saw works every round.
Scan, store, decide. Today these are three separate parts of the program. Keep them separate and you can test each one on its own.
The idea — lists and dictionaries 10 min
A list keeps things in order. It is perfect for “what was in slot 1, slot 2, slot 3, slot 4”:
seen = [] seen.append("red") seen.append("blue") print(seen) # ['red', 'blue'] print(seen[0]) # red (Python counts from 0) print(len(seen)) # 2
A dictionary is a lookup table. You give it a key and it gives back a value. It is perfect for “red objects go to zone A”:
ZONE = {"red": "A", "blue": "B", "green": "C", "yellow": "D"} print(ZONE["blue"]) # B
Python counts from 0. The first item is seen[0]. SPIKE counts from 1. This one difference causes many off-by-one bugs when you move across.
Blocks vs Python 15 min
SPIKE has lists, so the scan looks similar in both.
SPIKE word blocks
when program starts :: events hat delete all of [seen v] :: variables repeat (4) move [↑ v] for (10) [cm v] :: movement add ((E v) color :: sensors) to [seen v] :: variables end write (item (1) of [seen v] :: variables) :: display
Pybricks Python
# robot, hub, eye and colour_name are set up as before seen = [] for slot in range(4): robot.straight(100) seen.append(colour_name(eye.color())) print(seen) hub.display.char(seen[0][0])
| Block | Python | Watch out |
|---|---|---|
| delete all of seen | seen = [] | |
| repeat 4 | for slot in range(4): | slot counts 0, 1, 2, 3, not 1 to 4. |
| add (E color) to seen | seen.append(…) | |
| item 1 of seen | seen[0] | SPIKE's first item is 1. Python's is 0. |
The lookup is where blocks stop. SPIKE has no dictionary, so you would need a long chain of if-blocks.
SPIKE word blocks
Pybricks Python
ZONE = {"red": "A", "blue": "B", "green": "C", "yellow": "D"} for slot, colour in enumerate(seen): print("slot", slot, colour, "goes to", ZONE[colour])
| Block | Python | Watch out |
|---|---|---|
| (no block) | ZONE[colour] | A missing key stops the program with KeyError. |
| (no block) | enumerate(seen) | Gives the position and the item together. |
Write it from scratch 20 min
Set out four coloured objects in a row, 100 mm apart, beside a straight path. Shuffle them by rolling a die, not by choosing. Open a blank file, random_scan, and build three parts:
colour_name(c)— turns a Color from your lesson 15 table into a word. Returning a word makes the list easy to read in the terminal.- Scan. Drive past the four slots and fill
seen. Print it. Check it against the table by eye. - Decide. Build a
plan: a list of (slot, zone) pairs, in the order the robot will deliver them. Print the plan. Do not drive it yet.
The shape of colour_name
def colour_name(c): if c == MAT_RED: return "red" elif c == MAT_BLUE: return "blue" elif c == MAT_GREEN: return "green" elif c == MAT_YELLOW: return "yellow" return "none"
Plan for a bad read. If a slot reads "none", ZONE["none"] crashes the run. Use ZONE.get(colour), which gives None instead, and skip that slot. A skipped object loses a few points. A crashed program loses the run.
Test and measure 15 min
- Run the scan ten times, shuffling between runs. Write each printed list beside the real order. How many slots were read correctly out of 40?
- If a slot is wrong, is it always the same slot? That points at distance, not colour. Check your 100 mm spacing.
- Take one object away. Does your program skip the empty slot or crash?
- Now drive the plan for real, using your homed arm from lesson 13. Score one point per object in the right zone.
Break it, fix it 10 min
One mistake each. Read the error, or compare the output with the table.
Bug 1 — IndexError
seen = ["red", "blue", "green", "yellow"]
for slot in range(1, 5):
print(seen[slot])Bug 2 — only one colour ever remembered
for slot in range(4):
robot.straight(100)
seen = []
seen.append(colour_name(eye.color()))
print(seen)Bug 3 — KeyError: 'Red'
ZONE = {"red": "A", "blue": "B"}
colour = "Red"
print(ZONE[colour])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.
The rule changed. A new rule says: deliver the objects in the order red, blue, green, yellow, wherever they are. Change your decide step so that:
- it finds which slot each colour is in;
- it builds the plan in the new order;
- it prints the plan before driving, so you can check it.
Done means: the printed plan is right for three shuffles in a row, and you changed only the decide step.
Engineering journal 5 min
Today’s entry:
- a sketch of the scan path and the slot spacing
- your ten-scan results: correct reads out of 40
- what your program does when a slot reads nothing
- the 0-versus-1 counting rule, in your own words