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

First steps · Robot Quest

How do you tell a computer exactly what to do?

Course contents

Why you need it

A computer does nothing on its own. A program is a list of instructions, and the computer follows them one by one, exactly as written. Before learning any Python rules, you need this one idea: you decide every step.

In this course you control a small robot called Bit. Bit only understands a few commands, and it does exactly what you type, nothing more.

The intuition

Think of giving directions to a friend over the phone in a dark room: "Take one step forward. Another step. Stop." Your friend can't see the map, so every step has to be said out loud.

Bit is that friend. The command move() means "go forward one square". To go three squares, you say it three times:

move()
move()
move()

The round brackets () mean "do it now". Writing move without brackets is like saying the word "walk" without asking anyone to walk.

Definition

Program

A program is a sequence of instructions that a computer executes in order, from top to bottom.

Function call

A function call runs a named piece of behaviour; in Python you write the name followed by parentheses, like move().

Every robot command in this course is a function call.

Examples

Two steps to the flag

Run it in the lesson

Bit faces right (east). The flag is two squares ahead. Press Run and watch the highlighted line move with the robot.

move()
move()
Step by step
  1. L1First line: Bit moves one square forward. The program doesn't skip ahead.
  2. L2Second line: one more square. Bit is now on the flag.
  3. •No lines left, so the program ends. Bit stands on the flag: success.

One step too many

Run it in the lesson

What happens if you give one command too many? Run it and look at which line turns red.

move()
move()
move()
Step by step
  1. L1Bit moves onto the empty square.
  2. L2Bit moves onto the flag. The goal is reached, but the program isn't finished.
  3. L3There is a wall ahead. Bit follows the instruction anyway and crashes. The computer never guesses what you meant.

Common mistakes

Forgetting the brackets

move without () does nothing at all: you only named the command. Always write move().

Capital letters and spaces

Python is case-sensitive. Move() and move () with odd spacing are risky; Move() is an error because no command has that name. Type exactly move().

Quick quiz

1. Where does Bit end up?

move()
move()
  1. AOn the flag
  2. BOne square before the flag
  3. CIt crashes into the wall
Show the answer

Answer: B

Two move() calls means exactly two squares. The flag is three squares away, so Bit stops one square short.

2. Which line makes Bit crash?

move()
move()
  1. ALine 1
  2. BLine 2
  3. CNo crash
Show the answer

Answer: B

Line 1 moves onto the flag. Line 2 tries to move into the wall, so the error points at line 2.

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. PredictRead before you run

    Without running it, predict where Bit ends up.

  2. ModifyThe flag moved

    This program worked when the flag was 3 squares away. The flag is now 5 squares away. Change the program so Bit reaches it.

  3. WriteGoing down

    This time Bit faces down (south). move() always means forward, whichever way Bit faces. Write the whole program.

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.

Terms in this lesson

TermChineseDefinition
Program程序A sequence of instructions that a computer executes in order.
Instruction指令A single step that tells the computer to do one thing.
Function call函数调用Running a named action by writing its name followed by parentheses, such as move().