Bank Account Simulation

Implement a class representing a bank account, with methods to deposit and withdraw money. Raise custom exceptions for scenarios like withdrawing more money than is in the account.

Example 1:

Input: deposit: 1000, withdraw: 2000 
Output: "Error: Insufficient balance."

Example 2:

Input: deposit: 1000, withdraw: 500 
Output: "Transaction successful. Remaining balance: 500."

Use a class variable to keep track of the account balance.

class BankAccount:
    def __init__(self):
        self.balance = 0

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            raise Exception("Error: Insufficient balance.")
        else:
            self.balance -= amount
            return f"Transaction successful. Remaining balance: {self.balance}."

account = BankAccount()
account.deposit(1000)

print(account.withdraw(2000))  # Output: "Error: Insufficient balance."
print(account.withdraw(500))  # Output: "Transaction successful. Remaining balance: 500."

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!