LAB-8
Write a function named DivExp which takes TWO parameters a, b and returns
a value c (c=a/b). Write suitable assertion for a>0 in function DivExp and raise
an exception for when b=0. Develop a suitable program which reads two values
from the console and calls a function DivExp.
def DivExp(a, b):
if b == 0:
raise ValueError("Denominator cannot be zero")
assert a > 0, "Numerator must be positive"
return a / b
# Take user input
a = float(input("Enter the numerator: "))
b = float(input("Enter the denominator: "))
# Try to divide and handle exceptions
try:
result = DivExp(a, b)
print(f"The result of the division is {result}")
except ValueError as e:
print(e)
Algorithm:
1. Start
2. Define the function DivExp(a, b):
• If b (denominator) is 0:
• Raise a ValueError with the message "Denominator cannot be zero."
• If a (numerator) is less than or equal to 0:
• Raise an AssertionError with the message "Numerator must be
positive."
• Otherwise, return the result of a / b.
3. Take input from the user:
• Prompt the user to enter the numerator.
• Convert the input into a floating-point number and store it in a.
• Prompt the user to enter the denominator.
• Convert the input into a floating-point number and store it in b.
4. Perform division inside a try-except block:
• Try to:
• Call the DivExp(a, b) function.
• Store the returned result.
• Print the result of the division.
• If a ValueError is raised:
• Print the error message.
5. End
Output:
enter the numerator:5
enter the denominator:3
the result of the division is1.6666666666666667
enter the numerator: 7
enter the denominator:0
Denominator cannot be zero