🎯 What you will learn
- Sensors give a robot information
- If and else make a choice
- Good robots check before they move
👋 Let’s get started!
A robot cannot see like you do. It uses sensors: a distance sensor tells it how far away a wall is. The robot then uses if and else to decide what to do. 👀
🪜 Step by step
1. 📏 Distance sensor
The sensor gives a number: how far the wall is, in centimetres.
distance = 25
print("Wall is", distance, "cm away")
2. 🤔 Decide with if
If the wall is close, stop. Otherwise, keep going.
if distance < 10:
print("Stop!")
else:
print("Keep going")
3. 🔁 Check again
A good robot checks its sensor every time it moves.
for distance in [25, 8, 14]:
print(distance)
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Safe to go?
for distance in [25, 8, 14]:
if distance < 10:
print(distance, "cm: Stop! Wall close")
else:
print(distance, "cm: Keep going")🎨 Unplugged activity
🧱 Make a “robot maze” with cardboard boxes. Walk toward a box and stop when you are close, like a robot sensor. Count your steps!
Check before you move
distance = 6
if distance < 10:
print("Turn left")
else:
print("Go forward")📖 Key words
Tap a card to flip it over!
- Sensor
- A part that senses the world 👀
- Condition
- A question like distance < 10 ❓
- else
- What happens when the condition is not true 🔀
🏆 Challenge
Change the program so it prints “Stop!” when distance is less than 15, then press ▶ Run.
Challenge
distance = 12
Knowledge Check Quiz
- What is a sensor for?
- If distance is 25, which line runs?
- What does else do?