Tasting the soup
A cook tastes after adding salt, not only at the end.
10THINK LIKE A PROGRAMMER · TEST
Build one small step, run it, and check it with different values, even tricky ones.
IN REAL LIFE
Good makers test a little at a time.
A cook tastes after adding salt, not only at the end.
You fold, throw it softly, fix a wing, and throw again.
Before a ride down the hill, you test the brakes on flat ground.
LOOK FIRST
Write a little, run it, then add more. A new bug is in the new part.
Say what should appear before you run. Then compare.
Try 0, an empty list, the biggest, the smallest, and the exact border.
LOOK FIRST
== compares the result with what you expected.
def double(n):The function we want to test. return n * 2It gives back the number times 2.print(double(5) == 10)We expect 10. True means the test passed.print(double(0) == 0)A tricky value: 0. It passes too.RUN IT
Guess first. Then run real Python and check.
def can_ride(age):
return age >= 10
# Tests: True means passed
print(can_ride(12) == True)
print(can_ride(8) == False)
print(can_ride(10) == True)What will Python show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change >= to > and run again. Which test catches the change?
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
When is the best time to taste it?
When should you run it?
print(3 + 4 * 2)You expected 14. Python shows 11.
What do you do now?
def double(n):
return n + 2
print(double(5) == 10)What will Python show?
def share(candy, kids):
return candy // kidsWhich test could break it?
scores = []
best = scores[0]What happens?
if age > 10:
print("Ride!")Rule: kids 10 and older can ride.
Which test finds the bug?
Which ages make the best tests?
Rule: 10 and older can ride. The test with 10 shows False. Fix the border.
True True FalseOUTPUT
The test with 0 friends crashes. If there are no friends, share should return 0.
5 0OUTPUT
PyBot prints the result, but a test compares it. Change the last line so it shows True.
TrueOUTPUT
Want a fresh start? Clear only this page.
Run after each small step. Say what you expect, then compare. Try 0, empty lists, and the exact border.