Challenge: Bank Account

Lab Python Intermediate 20 points

Create a BankAccount class with a balance starting at 0 and methods deposit(amount) and withdraw(amount). A withdrawal bigger than the balance must be refused.

Process the commands and print what happens after each one.

Input

A number n, then n lines: deposit <amount> or withdraw <amount>.

Output

After each command: Balance: <balance>, or Insufficient funds if a withdrawal was refused.

Example

Input

4
deposit 100
withdraw 30
withdraw 100
deposit 5

Output

Balance: 100
Balance: 70
Insufficient funds
Balance: 75
Sample input available Sample output available
Editor
Python

Hints

Store the balance on self in __init__.

Have withdraw return True or False so you know whether it worked.

Split each command into the action and the amount.

Solution explanation

The rule “you can't withdraw more than you have” lives inside the class. The main loop reads each command, calls the matching method and prints the result. (Using if action == "deposit" works just as well as getattr.)

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

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

    def withdraw(self, amount):
        if amount > self.balance:
            return False
        self.balance -= amount
        return True


account = BankAccount()
for _ in range(int(input())):
    action, amount = input().split()
    ok = getattr(account, action)(int(amount))
    print(f"Balance: {account.balance}" if ok else "Insufficient funds")
Output
Answer
HOME LEARN COMMUNITY DASHBOARD