🌐 AI搜索 & 代理 主页
Skip to content
Open
Changes from 1 commit
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
Next Next commit
Add division.py with divide_numbers function and zero division valida…
…tion (fixes #13845)

This module provides a function to perform division with error handling for zero denominator, making it user-friendly for beginners.
  • Loading branch information
Aravind30648 authored Nov 24, 2025
commit 8b5bac12c093080e2f146d8f8e9b6cd17e950dc4
40 changes: 40 additions & 0 deletions maths/division.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
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`

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.")

Check failure on line 39 in maths/division.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/division.py:39:89: E501 Line too long (89 > 88)

Check failure on line 39 in maths/division.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/division.py:39:89: E501 Line too long (89 > 88)
return a / b
Loading