KnowledgeBoat Logo
|

Computer Science

Use assert statement in Question No. 3 to test the division expression in the program.

Python Exception Handling

3 Likes

Answer

numerator = float(input("Enter the numerator: "))
denominator = float(input("Enter the denominator: "))
assert denominator != 0, "Error: Denominator cannot be zero."
quotient = numerator / denominator
print("Quotient:", quotient)
Output
Enter the numerator: 12
Enter the denominator: 3
Quotient: 4.0

Enter the numerator: 5
Enter the denominator: 0
Traceback (most recent call last):
  File "c:\PythonPlayground\q3.py", line 3, in <module>
    assert denominator != 0, "Error: Denominator cannot be zero."
AssertionError: Error: Denominator cannot be zero.

Answered By

1 Like


Related Questions