×

python · level 2 · unit 4 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

checks a fact and complains loudly if it is false.

assert is followed by a condition. If the condition is True, nothing happens and the program carries on. If it is False, Python stops with an : an error you planned on purpose.

That makes assert perfect for a . Call your function with a value whose answer you already know, and assert the result. A wrong answer is caught now, not in front of a customer.

Add a message after a comma, and it is shown when the check fails.

def add_vat(price):
    return price * 1.2
assert add_vat(10) == 12, "10 plus VAT should be 12"
assert add_vat(0) == 0, "0 plus VAT should be 0"
print("All tests passed")

Tests are code that checks code, and professional programmers write them every day.