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 > 0

  • If not, program should stop with assertion error

2. Exception Handling

  • If b = 0

  • Raise ZeroDivisionError


Program Flow

  1. Read two numbers from user

  2. Call DivExp(a, b)

  3. Inside function:

    • Assert a > 0

    • If b == 0 → raise exception

    • Else → return a / b

  4. Handle exception in main program

  5. Print result

👉 Key Idea:
Assertion is for logical condition checking
Exception is for runtime error handling


Flowchart (Block Diagram of Logic)


Flowchart Explanation
  • Start

  • Read inputs

  • Call function

  • Check assertion (a > 0?)

  • Check if b == 0

  • Perform division

  • Return result

  • Handle exception

  • Stop


Algorithm

  1. Start

  2. Define function DivExp(a, b)

  3. Assert a > 0

  4. If b == 0

    • Raise ZeroDivisionError

  5. Else

    • Return a / b

  6. Read values from user

  7. Call function inside try block

  8. Print result

  9. Handle exceptions using except

  10. Stop


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

Enter value of a: 10
Enter value of b: 2
Result = 5.0

Case 2 – Assertion Failure

Enter value of a: -5
Enter value of b: 2
Assertion Error: Value of 'a' must be greater than 0

Case 3 – Division by Zero

Enter value of a: 10
Enter value of b: 0
Exception: Division by zero is not allowed

Important Concepts Used

  • Function definition

  • assert statement

  • raise keyword

  • try-except block

  • Exception classes

  • Return statement


Viva Voce Questions

  1. What is an assertion?

  2. When should we use assertion?

  3. What is difference between assertion and exception?

  4. What does raise do?

  5. What is ZeroDivisionError?

  6. What happens if try-except is not used?

  7. Can we create custom exceptions?



Procedure to Execute the Program

  1. Open Python IDE

  2. Create new file

  3. Type the program

  4. Save with .py extension

  5. Run the program

  6. 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.



-:END:-

Comments

Popular posts from this blog