-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathdivision.py
More file actions
42 lines (34 loc) · 1.18 KB
/
division.py
File metadata and controls
42 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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:
"""
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