Pseudocode and bugs · Computer Basics
How do you write instructions so precise that a computer could follow them, before you know any programming language?
Course contentsWhy you need it
Programmers often plan in plain words first and turn the plan into code afterwards. The plain-words version is called pseudocode.
It's built from just three kinds of step: do things in order, decide with if, and repeat. These are exactly what you'll write in the robot course next, and in every programming language after that. Here you can learn them with no rules about symbols to worry about.
The intuition
Pseudocode is instructions in everyday language, written as carefully as code. Almost every program is made of three kinds of step.
1. Sequence: one step after another.
put bread in the toaster
press the lever
wait until it pops up2. Decision: if something is true do this, otherwise do that.
if it is raining
take an umbrella
otherwise
wear sunglasses3. Repetition: do something again and again.
repeat 3 times
clap your handsor, when you don't know in advance how many times:
while the glass is not full
pour a little more waterThe indented lines (pushed in to the right) belong to the if or the repeat above them. Lines that are not indented happen once, after it.
You can also give a group of steps a name: "to make tea: boil water, put a tea bag in a cup, pour, wait 3 minutes". From then on, "make tea" is a new step you can use anywhere. That's called a function.
Bugs. When a step is wrong, missing or in the wrong place, the result is wrong. That's a bug, and finding and fixing it is debugging. Every programmer writes bugs every day; good programmers find them by tracing: following the steps one at a time, exactly as the computer would.
The machines on the right are the pseudocode above written in Python. Python reads almost like pseudocode, and you'll learn it soon.
Definition
Pseudocode
Pseudocode describes the steps of an algorithm in plain language, structured like code but not meant to be run by a computer.
Loop
A loop repeats a group of steps, either a set number of times or for as long as a condition is true.
Bug
A bug is a mistake in a program that makes it behave differently from what was intended.
Debugging is finding and fixing bugs.
Examples
Decision: umbrella or sunglasses
Try it yourselfThe decision from above, in Python. True means yes and False means no. Change it and run again: only one of the two indented lines runs, but Go outside always runs because it isn't indented.
raining = True # change me 改这里: True or False
if raining:
print("Take an umbrella")
else:
print("Wear sunglasses")
print("Go outside")- L1Is it raining? Yes.
raining = True - L2Check the condition: it's true.
true - L3So do the indented step under if.
Take an umbrella - L6The else part is skipped. This line isn't indented, so it always runs.
Go outside
Repetition: countdown
Try it yourselfA while loop: repeat as long as the condition is true. Start from 5 or 10 instead.
count = 3 # change me 改这里
while count > 0:
print(count)
count = count - 1
print("Lift off!")- L1Start the count at 3.
count = 3 - L2Is count more than 0? Yes, so do the indented steps.
3 > 0 - L3Say the number.
3 - L4Count down by one, then go back up to the check.
count = 2 - L2After 3, 2 and 1, count is 0 and the check fails.
0 > 0 is false - L5The loop is over.
Lift off!
Find the bug
Try it yourselfEvery step is here, but two are in the wrong order: you can't squeeze out toothpaste with the cap still on. Swap the first two lines (cut one and paste it below the other) and run it again.
print("Put toothpaste on the brush")
print("Take the cap off the toothpaste")
print("Brush for two minutes")
print("Rinse your mouth")- L1The computer happily does this first, cap still on.
Put toothpaste on the brush - L2Too late: this should have come first.
Take the cap off the toothpaste - L3The rest is fine.
Brush for two minutes
Common mistakes
Indentation decides what repeats
In repeat 3 times, only the indented lines repeat. Indent one line too many and it happens three times; one too few and it happens once. Python follows the same rule.
A computer can't fill in the obvious
"Brush your teeth" is enough for a person, who fills in the obvious steps. A computer needs every one: pick up the brush, take the cap off, squeeze… Missing steps are the most common bug of all.
Bugs are normal
Programs rarely work on the first try, for anyone. A bug is a puzzle, not a sign that you're bad at this. Trace the steps, find the one that's wrong, fix it, try again.
Quick quiz
1. What does this say?
temperature = 30
if temperature is above 25
say "Shorts"
otherwise
say "Jeans"Show the answer
Answer: A
30 is above 25, so the condition is true: only the if part runs. The otherwise part is skipped.
2. What is said, in order?
repeat 2 times
say "hip"
say "hooray"Show the answer
Answer: A
Only the indented line repeats: hip, hip. say "hooray" isn't indented, so it happens once, after the loop.
3. A recipe step says "add salt to taste". Why is that a problem for a computer?
Show the answer
Answer: A
A person uses judgement; a computer needs an exact amount, such as "add 2 g of salt".
Key points
- Programs are built from three kinds of step: sequence, decision (if / otherwise) and repetition (loops).
- Indentation shows which steps belong to an
ifor a loop. - Bugs are normal. Trace the steps one at a time to find them.
You've finished Computer Basics. Next, Robot Quest turns these same building blocks into real Python commands for a little robot.
Practice
3 short exercises: think it through, change something and see what happens, then do one yourself. Answers are checked the moment you submit.
- ThinkTrace the stickers
Trace this pseudocode one step at a time:
money = 5 while money is more than 0 buy a sticker for 2 money goes down by 2How many stickers does it buy?
- ChangeClap, clap, clap, bow
The goal is to clap 3 times and then bow once. But this machine bows after every clap. Run it and see.
Fix the bug so it prints:
clap clap clap bow - Your turnGuide the robot
Write your own algorithm for a robot. The robot
^is facing up.#is a wall andFis the flag:# # # # # . F # # . # # # ^ # # # # # #It understands three commands: M = move one square forward, L = turn left, R = turn right (turning doesn't move it).
Write the shortest list of commands that gets the robot onto the flag, for example
MLMR.
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 |
|---|---|---|
| Pseudocode | 伪代码 | Steps written in plain language but structured like code. |
| Sequence | 顺序 | Steps carried out one after another, in order. |
| Condition | 条件 | A question with a yes-or-no answer that decides which steps run. |
| Loop | 循环 | A group of steps repeated a set number of times or while a condition is true. |
| Function | 函数 | A named group of steps you can use again and again by its name. |
| Bug | 程序错误 | A mistake in a program that makes it do something unintended. |
| Debugging | 调试 | Finding and fixing bugs, often by tracing the steps one at a time. |