Lesson 31: Bus stops
Count each stop once
- The robot is a bus. It follows a straight black line, and each red mark across the line is a bus stop.
- The robot drives 25 mm at a time, so its down eye sees one red mark for 2 or 3 steps.
- To count each stop once, the program must remember what it saw on the last step.
Words for this lesson
| Word | Meaning |
|---|---|
| flag | a True/False variable that remembers something |
| edge | the moment a reading changes |
| count | a variable that goes up by 1 |
What the eye sees
| Step | Eye sees | Last step red? | Add 1? |
|---|---|---|---|
| 1 | black | no | no |
| 2 | red | no | yes |
| 3 | red | yes | no |
| 4 | black | yes | no |
| 5 | red | no | yes |
- Add 1 only when the eye sees red and the last step was not red.
A flag
from vexcode_vr import *
def main():
readings = ["black", "red", "red", "black", "red"]
count = 0
on_red = False
for r in readings:
if r == "red":
if not on_red:
count = count + 1
on_red = True
else:
on_red = False
brain.print(count)
vr_thread(main)
- The brain prints
2: two red marks, even though "red" is in the list three times.
In the exam
- Counting is a standard method of solution.
- 0478/22/M/J/24 Q6(b): count the input values that are more than 100. Mark scheme: set the counter to zero before the loop; add one to it only when the condition is true (selection inside the loop); output the counter after the loop. Your bus-stop counter follows the same pattern: count only when the robot moves onto a red tile.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
A robot bus follows a straight black line to the green depot. Each red mark across the line is a bus stop. Drive 25 mm at a time until the down eye sees green, and count the stops. Careful: the eye sees each red mark for several steps, so add 1 only when red starts: keep on_red, which remembers whether the last step was on red. Print Stops: and the count.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
Now the bus must stop at stop 3. Use the same counting, but loop while count < 3:, so the bus stops as soon as it reaches the third red mark. Then print Bus stop 3.
Нажмите Запустить, чтобы увидеть результат здесь.