Wednesday, 9 April 2025

GRADE 12 : PRACTICAL 5 : EXCEPTION HANDLING

 '''You are developing a banking application where users can

withdraw money from their accounts. Implement a try-except block

to handle the scenario when a user tries to withdraw 

more money than their account balance, or if they try to withdra

a negative amount.

'''

#Answer:

account_balance = 100000.0  # Assume initial balance

def menu():

    global account_balance

    while True :

        ch=int(input("""MENU :

1.Withdraw Money

2.Check Balance

3.Exit"""))

        if ch==1:       

                   

            try:

                withdrawal_amount = float(input("Enter withdrawal amount: ")) 

                if withdrawal_amount > account_balance : 

                    raise ValueError("Insufficient balance")

                elif withdrawal_amount <0:

                    raise ValueError("Negative withdrawal amount")

                else: 

                    account_balance -= withdrawal_amount  

            except ValueError as e: 

                print(f"Error: {e}") 

            except Exception as e:

                print(f"Unexpected error occurred: {e}")

            else:

                print(f"Withdrawal successful")

            finally :

                print(f"Current balance: {account_balance}")

        elif ch==2:

            print(f"Account balance : ",account_balance)

        else:

            break

menu()   


No comments:

Post a Comment