Setup, and your first algorithm
Get Python running on your computer, meet the robot's room, and write your first two functions. On the way you'll learn the most important idea in all of computing: an algorithm is a list of exact steps.
Start here
A computer does exactly what you tell it, step by step, and nothing else. A list of exact steps is called an algorithm.
The problem
Robo-Helper lives in a room made of squares. Right now it can't do anything at all. It can't even say hello. It also can't work out how far it can walk before it bumps into a wall. You're going to give it both skills. But first you need a way to talk to it: Python, and a terminal.
The idea in one picture
Imagine writing instructions for making a jam sandwich, then handing them to someone who follows them exactly, word for word. "Put jam on the bread." They put the closed jar on top of the loaf. You never said open the jar, or use a knife, or which slice. A computer is that person. It isn't stupid. It's literal. Your job is to write steps so exact that even a literal follower gets it right.
Words to know
- Algorithm
- An exact list of steps that solves a problem. A recipe is a loose algorithm. Code is a strict one.
- Terminal
- A window where you type commands to your computer instead of clicking. Also called the command line, shell, Command Prompt or PowerShell.
- Folder path
- The address of a folder, like
Documents/Learning robotics/topics. - Function
- A named, reusable block of code. You give it inputs, it gives back an answer with
return. - Loop
- Code that repeats.
whilerepeats as long as something is true.
Going deeper: everything below is the full lesson. Set aside time for the setup part: once it works, it works for every lesson after this one.
1. The problem
Here is the robot's world. It's a grid of characters. # is a wall, . is floor, B is the bin, R is the robot, and the small letters are coloured blocks (red, blue, green):
##########
#Rr......#
#..##....#
#..#..b..#
#....##..#
#g.......#
#.#....B.#
##########
By the end of the course the robot will put the red block in the bin when you ask it to. Today it needs two tiny skills:
- Say hello with its name.
- Count how many steps it can walk to the right before the next square is a wall.
Before any of that, you need Python working. Let's do setup first, carefully. Most beginners who give up on programming give up here, at setup, not at the code. So go slowly, and if something doesn't match what you see, read the "If it goes wrong" boxes.
Step A. Install Python 3
Python is the programming language we'll use. "Installing" it puts a program on your computer that reads your .py files and runs them. You need version 3.8 or newer. The newest one is fine.
If you're on a family or school computer, you may need a parent or teacher to type an admin password during install. That's normal.
Windows
- Open a web browser and go to python.org. Click Downloads. The site detects Windows and offers a big yellow button, "Download Python 3.x.x". Click it.
- Open the file you downloaded (it's in your Downloads folder, named something like
python-3.x.x-amd64.exe). - Before you click anything else: at the bottom of the first screen there is a checkbox labelled "Add python.exe to PATH" (older versions say "Add Python to PATH"). Tick it. This lets you type
pythonin any terminal. Forgetting it is the number one setup problem on Windows. - Click Install Now. Say yes if Windows asks for permission. Wait for "Setup was successful", then click Close.
- Check it worked (you'll learn how to open a terminal in Step B): type
py --versionand press Enter. You should see something likePython 3.13.1.
python opens the Microsoft Store, Windows is using a shortcut instead of your install. Use py instead of python3 everywhere in this course (for example py check.py 0). If py isn't found either, run the installer again, choose Modify or Repair, and make sure "Add to PATH" is ticked. Then close and reopen the terminal.macOS
- Go to python.org, click Downloads, and click "Download Python 3.x.x" (it detects macOS).
- Open the downloaded
.pkgfile and click Continue through the screens, then Install. Type your Mac password if asked. - When it finishes, a Finder window opens. You can close it.
- Check it worked: open Terminal (Step B) and type
python3 --version. You should see something likePython 3.13.1.
python3 pops up a box asking to install "command line developer tools", you can click Install. That also gives you a working python3. Always type python3, not python: on some Macs python doesn't exist.Linux (Ubuntu, Debian, Fedora, Raspberry Pi OS…)
- Python 3 is almost always already installed. Open a terminal (Step B) and type
python3 --version. - If you see
Python 3.8or higher, you're done. - If it says "command not found", install it. On Ubuntu, Debian or Raspberry Pi OS type
sudo apt install python3. On Fedora typesudo dnf install python3. It asks for your password. Nothing appears while you type a password: that's normal, just type it and press Enter.
Step B. Open a terminal
- Windows: press the Windows key, type
powershell(orcmd), press Enter. A dark window with a blinking cursor appears. - macOS: press Cmd+Space, type
Terminal, press Enter. - Linux: press Ctrl+Alt+T, or find "Terminal" in your apps menu.
The terminal shows a prompt, something like C:\Users\Sam> or sam@laptop ~ %. That tells you which folder you're "standing in" right now. You type a command after it and press Enter. Try typing python3 --version (on Windows, py --version).
Step C. Go into the project folder with cd
cd means "change directory" (directory is another word for folder). The project lives in topics/18-robot-brain/project/ inside the course folder. You need the terminal to be standing in that project folder.
- The easy way on macOS and Linux: type
cdand a space, then drag theprojectfolder from Finder or your file manager into the terminal window. The path appears. Press Enter. - The easy way on Windows: open the
projectfolder in File Explorer. Click in the address bar at the top, typecmd, press Enter. A terminal opens already inside that folder. - Typing it yourself: the folder name has a space in it, so put the whole path in quotes. For example:
# macOS / Linux (change the start to wherever your course folder is) cd "/Users/sam/Learning robotics/topics/18-robot-brain/project" # Windows cd "C:\Users\Sam\Documents\Learning robotics\topics\18-robot-brain\project"
Check you're in the right place. Type ls (macOS/Linux) or dir (Windows) to list the files. You should see check.py, homework, solutions and README.md. If you don't, you're in the wrong folder: try again.
cd .. goes up one folder. cd homework goes down into homework. Press the ↑ key to bring back the last command instead of retyping it. Press Tab halfway through a folder name and the terminal finishes it for you.Step D. Run the checker for the first time
Inside the project folder, type:
python3 check.py 0 # macOS and Linux
py check.py 0 # Windows
You haven't written anything yet, so you should see yellow dots. This is real output:
Lesson 0: lesson00_hello.py
… introduce: not written yet (Lesson 0: write this function)
… steps_to_wall: not written yet (Lesson 0: write this function)
0/2 passed
That's a success. Python works, the checker works, and it found your homework file. Yellow … means "not written yet". Later you'll see green ✓ (correct) and red ✗ (wrong, with a hint).
Now look at the robot's room. Go into the homework folder and run the world file:
cd homework
python3 world.py # Windows: py world.py
cd ..
You should see the room picture from the top of this page.
ls or dir to look around. "command not found" or "not recognized" means Python isn't on the PATH: go back to Step A. On old Windows terminals the colours may show as odd codes like [32m. That's harmless. Windows Terminal or PowerShell shows them properly.Step E. Pick a code editor
You write code in an editor and run it in the terminal. Two good free choices:
- IDLE came with Python. Open it from your apps, then File → Open, and pick
homework/lesson00_hello.py. Save with Ctrl+S (Cmd+S on a Mac). - VS Code (code.visualstudio.com) is what many professionals use. Open the
projectfolder with File → Open Folder. It even has a built-in terminal (Terminal → New Terminal), already in the right folder.
Avoid Word or Pages: they add hidden formatting that breaks code.
2. First principles: what is an algorithm?
Steps, exact steps
An algorithm is a finite list of exact steps that, followed one at a time, solves a problem. Three things make a list of steps a good algorithm:
- Every step is exact. No "you know what I mean". "Spread some jam" is vague. "Use the knife to scoop one spoonful of jam and spread it over the top face of slice 1" is exact.
- The order is right. Open the jar before scooping.
- It stops. "Keep spreading jam" never ends. "Spread until the whole face is covered" does.
A computer is the perfect literal follower. That's both its superpower (it never gets bored, never skips a step) and its weakness (it never fills in the gaps for you).
Your first robot algorithm, on paper
Here's a corridor. The robot R stands on square 1. The wall is on square 5. Squares are numbered from 0, left to right:
square: 0 1 2 3 4 5
. R . . . #
The robot walks right, one square at a time, and must stop on the last free square, just before the wall. How many steps does it take?
A human just looks and says "3". But a robot can't "just look". It only knows where it is and can check one thing at a time. So write it as exact steps:
1. Set steps to 0. Set x to the start square.
2. Look at the next square, x + 1. Is it the wall?
3. If it's NOT the wall: move there (x goes up by 1), add 1 to steps, go back to step 2.
4. If it IS the wall: stop. The answer is steps.
Follow it by hand, like a computer would, writing down x and steps each time:
x = 1, steps = 0 next square is 2. Wall is 5. 2 is not 5 → move
x = 2, steps = 1 next square is 3. Not the wall → move
x = 3, steps = 2 next square is 4. Not the wall → move
x = 4, steps = 3 next square is 5. That's the wall → stop
answer: 3
This style of checking, writing every variable at every step, is called tracing. Professionals do it whenever code surprises them.
Predict first: the robot starts on square 4 and the wall is on square 5. What's the answer? Guess, then open.
0. Trace it: x = 4, steps = 0, the next square is 5, which is the wall, so stop straight away. The loop body never runs even once. This "do nothing" case is called an edge case, and it's where most bugs hide. The checker tests exactly this case.
Why not just subtract?
You might spot a shortcut: answer = wall − start − 1. For 1 and 5 that gives 3. Correct! So why does the homework say "use a loop, not subtraction"?
Because a real robot doesn't know where the wall is in advance. It only knows "is the square in front of me free?" Next lesson you'll replace "is it the wall square?" with a real check on the room map, and in Lesson 2 that check becomes a sensor. Subtraction only works when you already know the answer. The loop works when you have to find out. That's how robots think: look, move, look again.
Turning steps into Python
Each line of the paper algorithm becomes a line of Python:
| Paper step | Python idea |
|---|---|
| Set steps to 0 | a variable: steps = 0 |
| Repeat while the next square isn't the wall | a while loop with a condition |
| Move, and count it | add 1 to two variables: x += 1 means "x becomes x + 1" |
| Stop and give the answer | return steps |
3. Play with it
Interactive: run the algorithm one step at a time
The same rule as the Python homework. Pick where the robot starts and where the wall is, then press Step to run one turn of the loop. Watch x and steps change, exactly like the trace above.
Try these: start 4, wall 5 (the edge case). Start 0, wall 10. Does the answer always equal wall − start − 1? It does here, because the corridor is empty. In Lesson 2 the corridor will have real walls in it, and only the "look, then move" version will still work.
4. Homework
Open homework/lesson00_hello.py in your editor. You'll see two functions. Each has a docstring (the text in triple quotes) that says exactly what it must do, and a line raise NotImplementedError(...). That line means "not written yet". Delete it and write your code in its place.
Function 1: introduce(name)
What it does: takes a name and returns (not prints!) the robot's greeting as a string.
Example: introduce("Robo") must give back exactly "Hello, I am Robo!". Capital H, a comma, one space after it, an exclamation mark at the end.
Hint 1
You need to glue the name into the middle of some fixed text. Python has a neat way to do that called an f-string.
Hint 2
An f-string starts with f before the quote, and anything inside { } is replaced by its value. f"I am {age} years old" becomes "I am 15 years old" when age is 15.
Hint 3
One line: return followed by an f-string that contains Hello, I am , then {name}, then !. Check the punctuation against the example character by character.
Function 2: steps_to_wall(start_x, wall_x)
What it does: the paper algorithm from section 2. Start at start_x, walk right one square at a time, stop on the square just before wall_x, and return how many steps that took. Use a loop, not subtraction.
Example: steps_to_wall(1, 5) → 3 (1 → 2 → 3 → 4, then the wall is next). Also steps_to_wall(4, 5) → 0, and steps_to_wall(0, 10) → 9.
Hint 1
You need two variables that change: where the robot is now, and how many steps it has taken. Set both up before the loop.
Hint 2
A while loop keeps going while its condition is true. The condition is "the next square is still before the wall". The next square is x + 1. "Before the wall" means less than wall_x.
Hint 3
steps = 0, x = start_x
while (next square) is less than wall_x:
move x forward one
add one to steps
return stepsRun it and see
From the project folder:
cd homework
python3 lesson00_hello.py # Windows: py lesson00_hello.py
cd ..
When both functions are right, you should see exactly this (real output):
Hello, I am Robo!
Steps to wall: 3
Check it
python3 check.py 0 # Windows: py check.py 0
Your first green ✓ looks like this. It's real output of the finished homework:
Lesson 0: lesson00_hello.py
✓ introduce
✓ steps_to_wall
2/2 passed 🎉 lesson complete!
If something is off, you get a red ✗ with a hint. For example, if you forget the exclamation mark the checker says:
✗ introduce: introduce("Robo") should return exactly "Hello, I am Robo!"
And if your loop goes one step too far:
✗ steps_to_wall: steps_to_wall(1, 5) should be 3
Read the hint, change your code, save, and run the checker again. That loop (write, run, read, fix) is what programming actually is.
Stretch
Write steps_to_wall_left(start_x, wall_x) where the wall is on the left (so wall_x is smaller than start_x) and the robot walks left. Trace steps_to_wall_left(6, 2) on paper first. (The checker doesn't test this one; test it yourself with print.)
5. Real-world challenge: the literal friend
You'll feel what it's like to be a programmer whose computer does exactly what you wrote.
- Pick a task that takes a minute: making a jam (or butter) sandwich, putting on a jacket, or drawing a house on paper. Use a blunt butter knife only, and ask an adult if you're using the kitchen.
- Write the instructions on paper, numbered, without your friend watching. Aim for 8–15 steps.
- Your friend follows them literally. Their job is to be a computer: do exactly what each line says, nothing more, and do something silly if a step is vague. "Put the bread on the plate" while the bread is still in the bag? The whole bag goes on the plate.
- Record every bug. For each step that went wrong, write: step number, what you meant, what they did, and what kind of bug it was: vague (unclear words), missing (a step you forgot), order (right step, wrong place), or never stops.
- Fix and run again. Write version 2 and repeat. Keep going until a run has zero bugs.
What to record: the number of bugs in version 1, version 2 and version 3, and which kind was most common. Most people find 5–10 bugs in version 1. What it teaches: computers aren't clever about what you meant. Precision is the whole job, and testing (your friend) finds bugs you couldn't see while writing. That's exactly why this course has check.py.
6. Your Robo-Helper so far
Lesson 0 ✓ Python works, the checker works, the robot can greet you and count steps to a wall
The robot can't move yet, but you've learned its habit of mind: look at the next square, decide, step, repeat. That idea comes straight back:
- Lesson 1 gives the robot real moves on the 2D grid (
move,safe_move). - Lesson 2 turns your
steps_to_wallloop intodistance_to_wall: the same "step until blocked" loop, but in any direction on the real room map. - In the Lesson 11 capstone,
RoboHelper.drive_tocalls thatdistance_to_wallbefore every move to decide how fast it's safe to go.
7. Common mistakes
- Printing instead of returning.
print(...)shows text on screen but gives nothing back, so the function returnsNoneand the checker saysintroduce("Robo") should return exactly "Hello, I am Robo!". - Tiny text differences.
"Hello I am Robo!"(no comma) or"Hello, I am Robo"(no !) are different strings to a computer. Literal, remember? - Off by one. Using
while x < wall_xwalks the robot onto the wall square and gives 4 instead of 3. The checker sayssteps_to_wall(1, 5) should be 3. Trace it on paper to see why. - Running from the wrong folder.
check.pylives inproject/, the lesson files inproject/homework/. "can't open file" means you're in the wrong one. - Forgetting to save. The checker reads the file on disk, not what's in your editor. Save first.
8. Check yourself
In your own words, what makes a list of steps an algorithm?
Every step is exact, the order is right, and it's guaranteed to stop with an answer. "Make a sandwich" isn't an algorithm. A numbered list a literal friend can follow without guessing is.
Predict: what does steps_to_wall(3, 9) return? Trace it.
5. x goes 3 → 4 → 5 → 6 → 7 → 8, which is five moves. At x = 8 the next square is 9, the wall, so it stops.
Predict: if introduce uses print and no return, what does x = introduce("Robo") put in x?
None. The greeting appears on screen, but the function hands back nothing. Every Python function without a return returns None.
Why does the homework forbid subtraction for steps_to_wall?
Subtraction needs you to already know where the wall is. A robot finds out by checking one square at a time. The loop version still works when the corridor has unknown walls in it, which is exactly what Lesson 2 needs.
You type python3 check.py 0 and get "can't open file 'check.py'". What's wrong?
The terminal is in the wrong folder. Use ls or dir to see where you are, then cd into the project folder (the one that contains check.py).
9. Go deeper (optional)
- Free robotics courses: a catalogue of free, high-quality courses for when you want more.
- TensorTonic: a practice gym where you implement ML ideas from scratch, just like this course's homework, but many more.
- Three Books and a Gym: a longer plan for young people who want to build AI, not just use it.