Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pythonmaths/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)