First steps · Robot Quest
How do you tell a computer exactly what to do?
Course contentsWhy 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 lessonBit faces right (east). The flag is two squares ahead. Press Run and watch the highlighted line move with the robot.
move()
move()- L1First line: Bit moves one square forward. The program doesn't skip ahead.
- L2Second line: one more square. Bit is now on the flag.
- •No lines left, so the program ends. Bit stands on the flag: success.
One step too many
Run it in the lessonWhat happens if you give one command too many? Run it and look at which line turns red.
move()
move()
move()- L1Bit moves onto the empty square.
- L2Bit moves onto the flag. The goal is reached, but the program isn't finished.
- 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()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()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.
- PredictRead before you run
Without running it, predict where Bit ends up.
- 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.
- 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
| Term | Chinese | Definition |
|---|---|---|
| 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(). |