Program7
7. Develop a function named DivExp which takes TWO parameters a, b, and returns a value c (c=a/b). Write a suitable assertion for a>0 in the function DivExp and raise an exception for when b=0. Develop a suitable program that reads two console values and calls the function DivExp.
Objective of the Program
To understand user-defined functions
To apply assertion statements
To handle runtime errors using exceptions
To separate logic using modular programming
Logical Construction (Think Before You Code)
We need two levels of validation:
1. Assertion Check
Inside function:
Ensure
a > 0If not, program should stop with assertion error
2. Exception Handling
If
b = 0Raise
ZeroDivisionError
Program Flow
Read two numbers from user
Call
DivExp(a, b)Inside function:
Assert a > 0
If b == 0 → raise exception
Else → return a / b
Handle exception in main program
Print result
👉 Key Idea:
Assertion is for logical condition checking
Exception is for runtime error handling
Flowchart (Block Diagram of Logic)
Start
Read inputs
Call function
Check assertion (a > 0?)
Check if b == 0
Perform division
Return result
Handle exception
Stop
Algorithm
Start
Define function
DivExp(a, b)Assert
a > 0If
b == 0Raise ZeroDivisionError
Else
Return
a / b
Read values from user
Call function inside
tryblockPrint result
Handle exceptions using
exceptStop
Python Program
⚠ Students must complete the missing parts.
# Program using function with assertion and exception
def DivExp(a, b):
assert ____________, "Value of 'a' must be greater than 0"
if b == ____________:
raise ____________("Division by zero is not allowed")
c = ____________
return c
try:
x = float(input("Enter value of a: "))
y = float(input("Enter value of b: "))
result = DivExp(__________, ____________)
print("Result =", result)
except AssertionError as ae:
print("Assertion Error:", ae)
except ZeroDivisionError as ze:
print("Exception:", ze)
Probable Output
Case 1 – Valid Input
Case 2 – Assertion Failure
Case 3 – Division by Zero
Important Concepts Used
Function definition
assertstatementraisekeywordtry-exceptblockException classes
Return statement
Viva Voce Questions
What is an assertion?
When should we use assertion?
What is difference between assertion and exception?
What does
raisedo?What is ZeroDivisionError?
What happens if try-except is not used?
Can we create custom exceptions?
Procedure to Execute the Program
Open Python IDE
Create new file
Type the program
Save with
.pyextensionRun the program
Enter values when prompted
Google Colab – Online Execution
👉 Paste Colab link here.
[https://colab.research.google.com/drive/15iq0zhbcabfXqw0OASAgQnusnzOPli-4?usp=sharing]
Program Explanation
Assignment for Students
Add finally block
Create custom exception
Validate both a and b
Convert into menu-driven program
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.

Comments
Post a Comment