Lesson 25: Find the door
Look before you move
- The robot is in a dark room. It cannot see the door, but it can measure.
- A wall is close, so the distance is short. Through the door the distance is long, to the wall far outside.
- The robot looks in 4 directions, keeps the 4 distances in a list, and chooses the longest.
Words for this lesson
| Word | Meaning |
|---|---|
| max | the biggest value: max([3, 9, 4]) is 9 |
| index | the position of a value in a list, from 0 |
| heading | the direction the robot faces: 0, 90, 180 or 270 |
Position and heading
| Position in the list | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Heading | 0 | 90 | 180 | 270 |
| Direction | up | right | down | left |
- The heading is the position × 90.
max and index in Python
from vexcode_vr import *
def main():
dists = [410, 900, 410, 410]
biggest = max(dists)
where = dists.index(biggest)
brain.print(biggest)
brain.new_line()
brain.print(where * 90)
vr_thread(main)
- The brain prints 900 and then 90: the longest distance is at position 1, which is heading 90.
In the exam
- 0478/22/O/N/23 Q3(b): write pseudocode with a single loop to find the lowest value in an array. In the exam you may not use
max()ormin(): keep the best value so far and compare each item with it, as in Arduino lesson 32. - Here the same idea finds the highest distance: start with
dists[0], and if a later distance is bigger, keep it and its position.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The robot is in the middle of a dark room with one door. It turns to heading 0, 90, 180 and 270 and measures the distance each time. Keep the 4 distances in a list dists with append. The door is where the distance is longest: find it with max(dists) و dists.index(...), and print Door at and its heading, for example Door at 90.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Now leave the room: after finding the door, turn to its heading and drive 50 mm at a time until the wall in front is 150 mm away. The robot must end in the green square outside the door.
اضغط تشغيل لرؤية المخرجات هنا.