steps so precise you could follow them without understanding a thing.
four lines, one of them hard
look at the card.
decide which colour it is.
write down the matching symbol.
wait for the next one.
one of these four will take you three weeks to hand over.
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.
5a full pass without a swap: done.
you just ran bubble sort. computers sort like this, only faster.
it ended because a full pass had no swap left. remember that.
a program is an algorithm written for a machine.
and it must be complete: the machine adds nothing.
a program is a list of instructions. something follows that list. exactly, and in order.
the list at work
one line at a time, top to bottom. every line changes what you see.
one line
same instruction, different values
from lifi_hardware import led
led.set_color(255, 0, 0)
led.set_color(0, 0, 255)
same instruction, different values, different result.
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)
two lines swapped is a different program.
with a pause, and without
the program is not broken. it is too fast for your eyes.
pause in the middle, or at the end
no line is missing. it is still 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)
one place to change instead of three.
3 · again
import time
from lifi_hardware import led
colours = [(255, 0, 0), (0, 255, 0),
(0, 0, 255), (255, 255, 0)]
for r, g, b in colours:
led.set_color(r, g, b)
time.sleep(1)
the loop you write, and what actually happens
the loop you write
import time
from lifi_hardware import led
colours = [(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0)]
for r, g, b in colours:
led.set_color(r, g, b)
time.sleep(1)
what actually happens
the same two lines, four times, one value and one second per pass.
4 · only if
import time
from lifi_hardware import led
colours = [(255, 0, 0), (0, 255, 0),
(0, 0, 255), (255, 255, 0)]
for r, g, b in colours:
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: the condition is always true, and the body has no way out.
"my program hangs" almost always means "my loop has no way out".
what does it cost?
correct is the minimum. after that, compare how the cost grows.
an error message
an error message is information, not a verdict.
read it out loud. most of the time it says exactly what is wrong.
now watch me get it 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 did not take it over. you copied it.
your turn
1one colour, on.
2four colours, in a loop.
3blinking, and you can change how fast.
number 3 is the real one: the speed must change in one place, without digging through the code.