Skip to content
Open
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
42 changes: 42 additions & 0 deletions maths/division.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Division Algorithm with input validation for zero denominator.

This module provides a function to perform division with proper
error handling for edge cases, especially for beginners learning
algorithms.
"""


def divide_numbers(a: int | float, b: int | float) -> float:

Check failure on line 10 in maths/division.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PYI041)

maths/division.py:10:39: PYI041 Use `float` instead of `int | float`

Check failure on line 10 in maths/division.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PYI041)

maths/division.py:10:23: PYI041 Use `float` instead of `int | float`
"""
Divide two numbers with validation for zero denominator.

This function performs division of 'a' by 'b' with explicit validation
to raise a ValueError when attempting to divide by zero. This makes the
function more user-friendly and helps beginners understand error handling.

Args:
a: The dividend (numerator)
b: The divisor (denominator)

Returns:
float: The result of dividing a by b

Raises:
ValueError: If b (denominator) is zero

Examples:
>>> divide_numbers(10, 2)
5.0
>>> divide_numbers(7, 2)
3.5
>>> divide_numbers(5, 0)
Traceback (most recent call last):
...
ValueError: Cannot divide by zero. Please provide a non-zero denominator.
"""
if b == 0:
raise ValueError(
"Cannot divide by zero. Please provide a non-zero denominator."
)
return a / b
Loading