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 isassert b != 0, "The divisor cannot be zero"; then it returnsa / b. - Read an integer
nand thennlines with two integers,a b. - For each line call
divideinside atry. If all goes well, showa / b = Rwith the result to 2 decimals. IfAssertionError as eis raised, showError:followed by the message (e).
3
10 4
5 0
-9 3
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