🎯 What you will learn
- A loop repeats checks
- A robot follows light and dark lines
- Loops make robots react quickly
👋 Let’s get started!
Line-following robots have a light sensor under them. Dark means “on the line”. Light means “off the line”. Every few moments, the robot checks again. That repeating check is a loop! ➖
🪜 Step by step
1. 🔦 Light sensor
Under the robot, a sensor sees black or white floor.
readings = ["black", "black", "white"]
2. 🔁 Check repeatedly
A for loop checks each reading in turn.
for reading in readings:
print(reading)
3. ↩️ React
If the reading is black, keep going. If it is white, turn back.
if reading == "black":
print("On the line")
else:
print("Turn back")
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Follow the line
readings = ["black", "black", "white", "black"]
for reading in readings:
if reading == "black":
print("On the line: keep going")
else:
print("Off the line: turn back")🎨 Unplugged activity
➖ Tape a long line on the floor (or draw one with chalk). Walk along it while a grown-up calls “black” or “white” from the list in the lesson. Turn when they say white!
Fix the follower
for reading in readings:
if reading == "black":
print("Keep going")
else:
print("Turn back")📖 Key words
Tap a card to flip it over!
- Loop
- Repeats steps 🔁
- Sensor reading
- What a sensor reports 🔦
- ==
- Checks if two things are equal ⚖️
🏆 Challenge
Print “Turn back” for every white reading in this list. Press ▶ Run.
Challenge
readings = ["white", "black", "white"]
Knowledge Check Quiz
- What does the loop do?
- Which reading means on the line?
- What does == mean?