A self-playing piano with a paper roll above the keys and an empty stool
who is playing?
image: ai-generated (gpt-image-2)

live: you are the receiver

1look at the card.
2decide which colour it is.
3write down its first letter.
4wait for the next card.
that was an algorithm,
and you were the machine that ran it.
steps so precise that you can follow them without understanding what they are for.

four lines, one of them hard

1look at the card.
2decide which colour it is.
3write down its first letter.
4wait for the next card.
three of these lines are easy for a program, but deciding is the hard part.

one more algorithm, live

1stand in a row.
2look at your right neighbour.
3if they are shorter than you, swap.
4at the end of the row, start again.
5if a full pass had no swap, stop.
what you just ran is called bubble sort,
and a computer can sort the same way.
it follows the same five rules, only millions of times faster.

bubble sort on six bars

the procedure knows it is done when a full pass finds nothing left to swap.
a program is an algorithm
written for a machine.
and it must be complete, because the machine adds nothing.
a program is a list of instructions
that a computer follows
exactly and in order.

the list at work

the machine runs one line after the other, from top to bottom, exactly as written.

one line

same instruction, different values

the instruction stays the same, and the values decide what the LED shows.

1 · one after another

import time from lifi_hardware import led led.set_color(255, 0, 0) time.sleep(1) led.set_color(0, 0, 255) time.sleep(1)
swap two lines, and you have a different program.

with a pause, and without

the program is not broken, it is only too fast for your eyes.

pause in the middle, or at the end

no line is missing, and still it is a different program.

2 · a value with a name

import time from lifi_hardware import led bright = 120 led.set_color(bright, 0, 0) time.sleep(1) led.set_color(0, bright, 0) time.sleep(1) led.set_color(0, 0, bright) time.sleep(1)
one place to change instead of three.

3 · again

import time from lifi_hardware import led colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] for r, g, b in colors: led.set_color(r, g, b) time.sleep(1)

the loop you write, and what actually happens

import time from lifi_hardware import led colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] for r, g, b in colors: led.set_color(r, g, b) time.sleep(1)
the same two lines run four times, with one value and one second per pass.

4 · only if

import time from lifi_hardware import led colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] for r, g, b in colors: led.set_color(r, g, b) if (r, g, b) == (255, 255, 0): time.sleep(3) else: time.sleep(1)
now the run depends on something that changes along the way.

when does this end?

import time from lifi_hardware import led while True: led.set_color(255, 0, 0) time.sleep(1) led.set_color(0, 0, 255) time.sleep(1)
never, because the condition is always true and the body has no way out.
"my program hangs" almost always means "my loop has no way out".
working alone is not enough.
two correct programs can still differ by far in how long they take.

what does it cost?

being correct is the minimum, and after that you compare how the cost grows.

an error message

an error message is information,
not a verdict.
read it out loud, because most of the time it says exactly what is wrong.

two ways to ask

never keep code
you cannot change.
every hand-over ends with a small change: another colour, another waiting time.
if you cannot change it, you have not taken it over but only copied it.