Lesson 2-3 · Bounce Driving
Lesson 2-3 · Bounce Driving
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Stopping at the line is fine — but what if the robot should keep going, bouncing off
each edge like a ball in a box? To do that, the robot has to make a decision every
moment: if I see the line, turn around; otherwise, keep driving. That's the
if/else statement.
Learning Objectives
By the end of this lesson you will be able to:
- Write
if/elsestatements that choose between two actions - Combine
while True,if/else, and a sensor read for continuous decisions - Use the logical operators
and,or, andnot, and writeis_either_on_line()around theor - Write the bounce as a test function in
robot.py, from a pseudocode outline
Making a decision with if/else
An if/else runs one block when a condition is true and a different block when it's
false — never both:
if condition:
# runs when the condition is True
else:
# runs when the condition is False
A non-robot example makes the shape clear:
temperature = 85
if temperature > 80:
print("It's hot outside!")
else:
print("It's comfortable.")
When an if/else runs, how many of the two blocks execute?
The bounce algorithm
The behavior in plain English:
loop forever:
read the sensor
if the sensor sees the line:
stop, then turn around
else:
keep driving forward
"Loop forever" is while True: — a loop whose condition is always true, so it runs
until you power the robot off. That's completely normal in robotics; real robots run
a loop like this the whole time they're on.
What does `while True:` do?
Combining conditions — and the function that asks
One sensor can miss the line if the robot hits it at an angle. Checking both is
safer — and that's where or comes in: it's true if either side is true, so
get_left() > THRESHOLD or get_right() > THRESHOLD means at least one sensor sees
the line.
The three logical operators: and (both must be true — you used it in Lesson 2-2),
or (at least one true), not (flips true/false).
That or question — is either sensor on a line? — is going to be asked by the
bounce, by next lesson's random bounce, and by the turns in Lesson 2-9. So it becomes
the next function in your LINE SENSOR section. It returns True or False: a
Boolean is a value like any other, and return can hand one back just as it handed
back a number in Lesson 1-11. The name starts with is_ because it answers a yes/no
question — a convention you'll see in every function like it from here on.
left is 0.2 and right is 0.9 (THRESHOLD 0.5). Is `left > THRESHOLD or right > THRESHOLD` True?
This lesson's additions to robot.py
One function for the LINE SENSOR section — add it under get_right() — and one test
to paste above RUN. The test is the exercise again: the pseudocode from above is
inside it as comments.
def is_either_on_line():
# return True when AT LEAST ONE reading is above THRESHOLD
pass
def test_bounce():
# loop forever:
# if the left sensor sees the line:
# stop, print a message, turn around
# else:
# drive forward
pass
Activity · Build the bounce
Step 1 · One sensor
Turn the comments in test_bounce() into code, with get_left() > THRESHOLD as the
condition, and set the run line to test_bounce(). Start the robot inside the circle,
press the button, and watch it bounce back and forth. If it drives through the line,
check which way your comparison points — the sensor reads high on the line.
Step 2 · Either sensor
Write the body of is_either_on_line(). Check it before it drives anything: add
" On line:", is_either_on_line() to the print in test_sensors(), run that, and
slide the robot by hand — True with either sensor on tape, False with both off.
Then change the condition in test_bounce() to call it, and watch the robot bounce
whichever sensor hits first.
Step 3 · Challenge — count the bounces
Add a bounce_count variable that goes up by one each bounce, and print it. You'll
build on counters like this later.
Real-world connections
if/else decisions are the backbone of anything that reacts:
Self-driving cars
"If an obstacle is ahead, brake; otherwise keep going" — millions of these checks per second.
Smoke alarms
If smoke is over the threshold, sound the alarm; else stay quiet.
Form validation
If the password is too short, show an error; otherwise let you continue.
Wrap-up
- How many branches of an
if/elserun each time? (Exactly one.) - What does
while Truemean, and how do you stop it? (Loop forever; stop the robot.) - What's the difference between
andandor? (andneeds both true;orneeds at least one.)