From fbe87d2716ebb3e8f2e6f7fcfae4447bb052a6c7 Mon Sep 17 00:00:00 2001 From: EOBRIEN2502 Date: Tue, 3 Mar 2026 10:04:27 +0000 Subject: [PATCH 1/2] feature: zero division implementation and tests --- pythonmaths/arithmetic.py | 9 ++++++++- tests/test_arithmetic.py | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pythonmaths/arithmetic.py b/pythonmaths/arithmetic.py index a26c2db..9869b50 100644 --- a/pythonmaths/arithmetic.py +++ b/pythonmaths/arithmetic.py @@ -51,8 +51,15 @@ def divide(x: int | float, y: int | float) -> float: 5.0 >>> arithmetic.divide(5, 2) 2.5 + + I've added a try and except code block to catch a divide by zero error. """ - return x / y + try: + return x / y + except ZeroDivisionError as e: + raise ZeroDivisionError( + "You can not divide by 0, please choose another value for 'y'." + ) from e def multiply(x: int | float, y: int | float) -> float: diff --git a/tests/test_arithmetic.py b/tests/test_arithmetic.py index f3d46b8..fa2c01a 100644 --- a/tests/test_arithmetic.py +++ b/tests/test_arithmetic.py @@ -64,3 +64,10 @@ def test_multiply(x: int | float, y: int | float, expected: int | float) -> None def test_divide(x: int | float, y: int | float, expected: int | float) -> None: """Test the divide function.""" assert arithmetic.divide(x, y) == pytest.approx(expected) + +def test_divide_zero_division_exception() -> None: + """Test that a ZeroDivisionError is raised by the divide() function.""" + with pytest.raises(ZeroDivisionError): + arithmetic.divide(2, 0) + + From e41442ebeb89d6dbbd673460476d2a26031e27e1 Mon Sep 17 00:00:00 2001 From: EOBRIEN2502 Date: Tue, 3 Mar 2026 10:23:58 +0000 Subject: [PATCH 2/2] Empty commit