Turning · Robot Quest
How do you change direction, and how does a program describe it?
Course contentsWhy you need it
Real paths are rarely straight. To get around corners Bit needs to turn. Turning also teaches an important habit: you must keep track of the state of things (here, which way Bit is facing), because the same command does different things in different states.
The intuition
Stand up and face the window. "Turn left" means your left, not the left side of the room. After you turn, "forward" points somewhere new.
Bit works the same way:
turn_left()turns 90° to Bit's left, staying on the same square.turn_right()turns 90° to Bit's right.move()then goes forward in the new direction.
Look at Bit's eyes in the picture: they always show which way is forward.
Definition
State
The state of a program is the information it remembers at a given moment, such as the robot's position and direction.
turn_left() doesn't move Bit; it only changes the direction part of the state.
Examples
Round the corner
Run it in the lessonBit walks to the corner, turns right to face down, and walks to the flag.
move()
move()
turn_right()
move()
move()- L1Facing east, move to the middle square.
(1,2) → - L2Move again: Bit reaches the corner.
(1,3) → - L3Turn right. Bit stays on the corner but now faces south.
(1,3) ↓ - L4Forward is now down, so Bit moves down one square.
(2,3) ↓ - L5One more square down: the flag.
(3,3) ↓
Left, left = turn around
Run it in the lessonTwo left turns make Bit face the opposite way. Try changing them to two right turns: does anything change?
move()
turn_left()
turn_left()
move()
move()
move()- L1Bit starts facing west (<) and moves one square towards the flag.
- L2First left turn: now facing south.
- L3Second left turn: now facing east, the opposite of where it started.
- L4Bit moves east, away from the flag. The robot does what you wrote, not what you meant.
- L6Line 6 hits the wall: this example is a bug on purpose. Fix it so Bit reaches the flag.
Common mistakes
Screen left vs robot left
If Bit faces down, turn_left() makes it face right on the screen. Imagine yourself standing in Bit's place.
Turning is not moving
After turn_right() Bit is still on the same square. You still need move() to go anywhere.
Quick quiz
1. Bit faces east (right). After turn_left(), which way does it face?
Show the answer
Answer: A
Facing east, your left hand points north.
2. Where does Bit end up?
move()
move()
turn_left()
move()Show the answer
Answer: B
At the corner Bit turns left, so it faces up (north). Above the corner is a wall.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWhich way now?
Predict where Bit ends up.
- ModifyMirror image
The program below solves a corner that bends down. The new map bends up instead. Change one line.
- WriteZigzag
Guide Bit down the zigzag to the flag.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| State | 状态 | The information a program remembers at a moment, such as position and direction. |
| Sequence | 顺序执行 | Running instructions one after another, in the order they are written. |
| Bug | 程序错误 | A mistake in a program that makes it behave differently from what you intended. |