Lesson 4-5 · Turning
Lesson 4-5 · Turning
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Right now turn_to() is a stub: the robot faces whichever way it's told, instantly. A
real robot turns a quarter turn at a time, with the Module 2 turn_clockwise(). Today
you write the real turn_to(), with one more stub standing in for the turn itself, so
every quarter turn shows up as a printed line.
Learning Objectives
By the end of this lesson you will be able to:
- Design a turn as a loop: one clockwise quarter turn at a time until the heading matches
- Wrap the heading from 3 back to 0 with
(heading + 1) % 4 - Write the real
turn_to()and count its turns in the delivery run's printout
Turning until you face it
turn_to(current_heading, desired_heading) knows both headings. Each clockwise quarter
turn adds 1 to the heading, so the robot can simply turn clockwise while it isn't facing
the right way:
# while current_heading is not desired_heading:
# one clockwise quarter turn
# current_heading goes up by 1, wrapping 3 back to 0
Adding 1 isn't enough on its own when the robot turns from West to North: 3 + 1 is 4, and
4 isn't a heading. The remainder operator % from Lesson 2-5 wraps it:
heading = (heading + 1) % 4
For 1, 2 and 3 the remainder is the number itself, and 4 % 4 is 0, so after West comes
North. Trace the loop from South (2) to East (1):
| pass | heading before | heading after, (h + 1) % 4 | facing East? |
|---|---|---|---|
| 1 | 2 (S) | 3 (W) | no |
| 2 | 3 (W) | 0 (N) | no |
| 3 | 0 (N) | 1 (E) | yes, the loop ends |
Three passes. The loop never asks which way is shorter: it always goes clockwise, so to face what's a quarter turn to its left it turns three quarters of the way around. It works, and it's simple; Lesson 4-6 has an optional challenge to make it quicker.
If the robot already faces the right way, the condition is False on the first check and
the loop does nothing, with no special case. At the end the function returns
current_heading, which by then equals desired_heading.
The robot faces West (3) and makes one clockwise turn. What does (heading + 1) % 4 give?
This lesson's skeleton
Replace the turn_to() stub function with this, and add the new stub next to the
fake_track_to_intersection() function:
def fake_turn_clockwise():
"""STUB for turn_clockwise(): print instead of
turning. Lesson 4-6 swaps in the real one."""
print("turning clockwise")
def turn_to(current_heading, desired_heading):
"""Turn clockwise a quarter turn at a time until
current_heading is desired_heading; return the new
heading."""
# while current_heading is not desired_heading:
# fake_turn_clockwise()
# current_heading goes up by 1, wrapping 3 back to 0
# return current_heading
pass
Activity · Predict, then count
Step 1 · Predict
The robot starts facing North. Before you write anything, work out how many clockwise
turns turn_to() makes before each of Leg 1's five moves (E, E, N, N, N).
Step 2 · Write it and run it
Write turn_to() from its outline. Run test_delivery().
What correct looks like: the same output as last lesson, with each turning: line
replaced by zero to three turning clockwise lines. Leg 1's turn counts match your
prediction. Every turn you count is three or fewer, and the run still ends with
new position: (0, 0) heading: S.
A student's printout shows turning clockwise forever after the first move. What's the likely bug?
Wrap-up
- How does
turn_to()know when to stop? (Its loop runs until the heading equals the one it wants; already facing it means zero turns.) - Why
(heading + 1) % 4? (Each clockwise turn adds 1, and% 4wraps 4 back to 0.) - Why does the robot sometimes turn three times? (The loop always turns clockwise, so a quarter turn left takes three quarter turns right.)