Everyone Can Learn Program
中文 Start learning
Course 1 · Zero to codingLesson 2 of 10Free to read

Turning · Robot Quest

How do you change direction, and how does a program describe it?

Course contents

Why 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 lesson

Bit walks to the corner, turns right to face down, and walks to the flag.

move()
move()
turn_right()
move()
move()
Step by step
  1. L1Facing east, move to the middle square.(1,2) →
  2. L2Move again: Bit reaches the corner.(1,3) →
  3. L3Turn right. Bit stays on the corner but now faces south.(1,3) ↓
  4. L4Forward is now down, so Bit moves down one square.(2,3) ↓
  5. L5One more square down: the flag.(3,3) ↓

Left, left = turn around

Run it in the lesson

Two 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()
Step by step
  1. L1Bit starts facing west (<) and moves one square towards the flag.
  2. L2First left turn: now facing south.
  3. L3Second left turn: now facing east, the opposite of where it started.
  4. L4Bit moves east, away from the flag. The robot does what you wrote, not what you meant.
  5. 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?

  1. ANorth (up)
  2. BSouth (down)
  3. CWest (left)
Show the answer

Answer: A

Facing east, your left hand points north.

2. Where does Bit end up?

move()
move()
turn_left()
move()
  1. AOn the flag
  2. BIt crashes on line 4
  3. CAt the corner, facing down
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.

  1. PredictWhich way now?

    Predict where Bit ends up.

  2. ModifyMirror image

    The program below solves a corner that bends down. The new map bends up instead. Change one line.

  3. 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

TermChineseDefinition
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.