Lesson 3-2 · Driving Multiple Intersections
Lesson 3-2 · Driving Multiple Intersections
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Reaching the first intersection is easy. Reaching the third is the same call,
three times — and that is what a for loop is for. This lesson turns "arrive at an
intersection" into a building block you can repeat a chosen number of times — the
first real function in the GRID DRIVING section.
Learning Objectives
By the end of this lesson you will be able to:
- Drive through one intersection to reach the next
- Use a
forloop to drive a chosen number of intersections - Write
drive_intersections(count)from its docstring, test it, and use it
This lesson's skeleton
Paste this into robot.py above RUN, at the end of the GRID DRIVING section. One
function outline and two tests.
# --- Lesson 3-2: a run of intersections ---
# drive_intersections(count) is track_to_intersection() in a
# loop.
# It ends parked on the count-th intersection, so Lessons 3-3
# and 3-4 build every path out of it and the two turns.
def drive_intersections(count):
"""Arrive at count intersections in a row, printing
which one it is each time. Ends parked on the last one
with the sensors past it, ready to turn or to go on."""
pass
def test_two_by_hand():
# track_to_intersection() written out twice, no loop: the
# robot should end parked on the SECOND intersection
pass
def test_drive_run():
# drive_intersections(3), then print that it is done
pass
The function has a docstring (Lesson 1-12) instead of step comments: the string
under the def line says what the function does and what ends it, and that's all
the help you get. The steps are yours to work out — this page tells you enough to.
Keep the docstring when you write the body; it belongs to the function.
Drive a set number with a for loop
Two calls in a row reach two intersections: each one ends with the sensors past the
tape (Lesson 2-8), so the next has a line to follow. But writing the call out by hand
for each intersection is repetitive, and it's wrong the moment you want a different
number. A for loop over range(count) repeats it a chosen number of times — the same shape as polygon() in Module 1, a loop and a
count, packaged into a function with the count as its parameter.
Nothing in the loop body changes from one pass to the next; the only thing the loop variable is good for is the print ("Intersection 1 of 3", and so on — remember it starts at 0). And there is no special case for the last intersection: each call ends with the robot's center on the cross, which is exactly where you want to be.
After two track_to_intersection() calls in a row, where is the robot?
Activity · Two by hand, then the helper
The skeleton is pasted in. For every step the robot starts on a line segment, facing the next intersection, with at least three intersections ahead of it.
Step 1 · test_two_by_hand()
Write the body: track_to_intersection() twice, no loop, and a print at the end. Set
the run line to test_two_by_hand() and run it. The robot should end parked on the
second intersection. Don't go on until it does.
Step 2 · drive_intersections(count)
Now write the function from its docstring: one parameter, a for loop over it,
track_to_intersection() inside, and a print so you can follow along. Write the body of
test_drive_run() — one call, drive_intersections(3), and a print — set the run
line to it, and run. The robot should arrive three times, printing each one, and end
parked on the third intersection.
Now drive_intersections(4) drives four segments in one clean line — the building
block the next two lessons are made of.
Real-world connections
"Do this a set number of times, then stop" is the rhythm of a lot of automation:
Floor counting
An elevator counts floor sensors as it rises and stops on the Nth — same detect-and-count idea.
Indexing lines
Assembly lines advance a fixed number of steps between stations, clearing each position.
Counting stops
Automated shuttles track how many stops they've passed to know when to pull over.
Wrap-up
- What loop drives a set number of segments? (A
forloop overrange(count), withtrack_to_intersection()inside.) - Where is the robot after the last pass? (Parked on the last intersection: every track ends with the robot's center on the cross.)