Command Palette

Search for a command to run...

Hectal
Phase 11: Testing and Code Quality
Python · Exercise 1 of 6Next
Intermediate

Write a check that catches broken code

shipping_fee(order_total) follows this rule: orders of 500 or more ship free (fee 0), smaller orders pay a fee of 40.

Write check_shipping_fee(fn). It receives any function fn that claims to follow the rule and uses assert to check it with several inputs, including the values on and next to the boundary. It must pass silently for a correct function and **raise AssertionError** for a wrong one.

Each assert looks like assert fn(500) == 0: call fn with an order total and compare the result with the fee the rule demands. Write a message after a comma if you like.

Your check will be run against the correct function and against three broken ones: one that starts free shipping one unit too late, one with the wrong fee, and one that stops giving free shipping for very large orders. The last two lines of the starter run your check on the correct function and print shipping_fee passed all checks.

What Check tests

  1. The correct shipping_fee passes your check
  2. A function with > instead of >= is caught
  3. A function with the wrong fee is caught
  4. A function that fails for very large orders is caught
  5. Your check uses assert

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.