Debugging and testing · 13 / 14 Exercise Easy

Preconditions with assert

Objective

Create a Python program with a function that checks its preconditions with assert and an error message.

Instructions

  • Define divide(a, b). Its first line is assert b != 0, "The divisor cannot be zero"; then it returns a / b.
  • Read an integer n and then n lines with two integers, a b.
  • For each line call divide inside a try. If all goes well, show a / b = R with the result to 2 decimals. If AssertionError as e is raised, show Error: followed by the message (e).
Input
3
10 4
5 0
-9 3
Output
10 / 4 = 2.50
Error: The divisor cannot be zero
-9 / 3 = -3.00

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise