Lesson 7: Decide with if
Decide with if
iflets the robot choose. It looks with a sensor, then does one thing or the other.elseis what happens when theiftest is false.
Words for this lesson
| Word | Meaning |
|---|---|
| if | do this only when the test is true |
| else | otherwise, do this |
| open | no wall close in front |
| blocked | a wall close in front |
Which way is open?
from vexcode_vr import *
def main():
if front_distance.get_distance(MM) > 500:
brain.print("open")
else:
brain.print("blocked")
vr_thread(main)
brain.printwrites on the robot's screen, shown under the playground.- The test
> 500asks: is the wall more than 500 mm away?
In the exam
Past paper 0478/12/F/M/23 Q6(c): a robot moves forward until an object is 10 cm or less in front of it; then it turns 90 degrees right and tries again. Explain how the sensor and the microprocessor automate this robot. The mark scheme's points, in your program:
- An infra-red or proximity sensor (your
front_distance) continuously sends the distance to the microprocessor. - The microprocessor compares the reading with the stored value (10 cm), like your
iftest. - Greater than 10 cm: it sends a signal to move forward. 10 cm or less: it sends a signal to turn.
- An actuator (the motors) moves or turns the robot, and the whole process repeats until the robot is stopped.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
One side is blocked by a wall, the other is open. Turn left and look. If the distance is more than 500 mm, drive 800 mm. Else turn round (180 degrees) and drive 800 mm the other way.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Drive up to the wall (stop at 100 mm), turn right, then drive to the next wall the same way. Write a function drive_to_wall() and call it twice. The goal is in the corner.
Haz clic en Ejecutar para ver la salida aquí.