Lesson 3-4 · Final Project
Lesson 3-4 · Final Project
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Time to tie the module together. Your robot will drive a complete square on the
grid — two intersections per side, turn right at each corner, four times — and end
up right back where it started. Same robot.py, no new Python: just your sections,
put to work.
Learning Objectives
By the end of this project you will be able to:
- Design a test function that drives a closed square path on the grid
- Use a
forloop to repeat a drive-and-turn sequence - Test and tune a real robot until the path closes
- Verify the robot returns to its starting position
The mission
Make the robot drive a square: each side is 2 intersections, with a right turn at each of the 4 corners. Done right, it finishes where it began.
Your program must: use the LINE TRACKING section and drive_intersections()
unchanged, drive the square with a for loop (not four copied blocks), print
progress, and return to start.
This lesson's skeleton
Paste this into robot.py above RUN, at the end of the GRID DRIVING section. One
setting and two tests; the second test is the project.
# --- Lesson 3-4: final project, the square ---
# The module project: four sides of SIDE intersections with a
# right turn at every corner, back to the start. Built from
# drive_intersections() and one turn; nothing new is defined.
SIDE = 2 # intersections per side
def test_one_side():
# one side of the square: drive SIDE intersections, then
# turn right
pass
def test_square():
# the project. A for loop that runs one side per pass,
# printing which side it is on; then the finish message
pass
SIDE is a setting, in capitals like the others, so the size of the square is one
number at the top of the sub-section and not a literal buried in a loop.
One loop, four sides
The whole square is a four-iteration loop: each pass drives one side, then turns the
corner. The piece that drives a side is drive_intersections(count) — you built and
tested it in Lesson 3-2 — so the loop body is a call to it and a turn.
Sketch the loop body in English first: what happens once per side? Then write it. The loop body is two calls and a print or two; if yours is longer, ask what the extra lines are for. There is nothing between the drive and the turn: the helper leaves the robot parked on the corner, and the turn ends with a sensor on the next side's line.
Four right turns add up to a full 360°, which is why the robot comes back around to its starting heading as well as its starting intersection.
The square loop runs turn_clockwise() once per side, four times. How many total degrees does the robot turn?
Activity · Test one side at a time
Don't run all four sides first. Build up, one test at a time:
- One side — write
test_one_side()and set the run line to it. The robot should driveSIDEintersections, pivot on the corner, and stop facing along the next side. Don't go on until it does. - Two sides — write
test_square()withrange(2)for now. What shape should that give you? Predict, then run. - Full square — change it to
range(4); does it close back to the start?
A student wrote `for leg in range(3):` and added drivetrain.straight(8.75, 0.5) right before turn_clockwise(). What are the two bugs?
Activity · Extend it
Once the square works, try a variation:
3-per-side square
Set SIDE = 3 for a larger square. Does it still close?
Alternating legs
Write test_rectangle(): a list [2, 3, 2, 3] indexed by the loop variable, so opposite sides differ.
Left-turn square
Swap in turn_counterclockwise() to drive the square the other way around.
Activity · Reflect
Demo your square and think back:
- What was hardest to get working, and how did you fix it?
- Why is the
forloop better than copying the side code four times? - How is this different from the polygon function in Module 1? (There you set distances and angles; here you count intersections and follow real lines.)
Nice work — Module 3 done 🎉
You reused functions you wrote weeks ago on a brand-new surface, learned to count
intersections, and sequenced drives and turns into real paths — and robot.py grew a
GRID DRIVING section with one function and a record of every test it passed. Every
route here was one you planned and hard-coded. That's about to change.