Start learning free

LESSON 406 OF 410 · PROJECTS: OBJECT-ORIENTED

Bank Account - Protect the Balance in Python

Practice

Build a BankAccount class that keeps its balance private and lets other code change it only through methods that check the rules. This is encapsulation: the object protects its own data.

Private balance. Python has no private keyword. By convention, store the balance in an attribute that starts with an underscore, self._balance, to tell other code "hands off".

What the class must have (Python):

  • __init__(self, initial_balance): creates an account that starts with the given balance.
  • deposit(self, amount): adds amount to the balance. If amount is zero or negative, do nothing (no error, the balance stays the same).
  • withdraw(self, amount): if amount is zero or negative, or larger than the balance, change nothing and return False. Otherwise subtract it from the balance and return True. Withdrawing exactly the whole balance is allowed (the balance becomes 0).
  • get_balance(self): returns the current balance. It is the only way for outside code to read it.

The driver. The driver at the end of the starter code is already written and uses your class. It runs these checks:

account = BankAccount(100)
account.deposit(50)
account.withdraw(30)
account.withdraw(200)
print("Balance: " + str(int(account.get_balance())))
account.deposit(-20)
account.deposit(0)
account.withdraw(-5)
print("Balance: " + str(int(account.get_balance())))
if account.withdraw(120):
    print("Withdrew all")
print("Balance: " + str(int(account.get_balance())))
if not account.withdraw(1):
    print("Rejected")

Walk through it: start with 100, deposit 50 (150), withdraw 30 (120), withdraw 200 is refused (still 120). Then deposit -20, deposit 0 and withdraw -5 are all ignored (still 120). Withdrawing 120 succeeds and empties the account, so the balance is 0, and withdrawing 1 more is refused.

Expected output:

Balance: 120
Balance: 120
Withdrew all
Balance: 0
Rejected

Both deposit and withdraw start with a guard that rejects amount <= 0. Write the guard first, then the change to the balance.

In __init__, write self._balance = initial_balance. Methods read and change it as self._balance.

if amount <= 0 or amount > self._balance: return False, then self._balance -= amount and return True.

class BankAccount:
    def __init__(self, initial_balance):
        self._balance = initial_balance

    def deposit(self, amount):
        if amount <= 0: return
        self._balance += amount

    def withdraw(self, amount):
        if amount <= 0 or amount > self._balance: return False
        self._balance -= amount
        return True

    def get_balance(self):
        return self._balance

account = BankAccount(100)
account.deposit(50)
account.withdraw(30)
account.withdraw(200)
print("Balance: " + str(int(account.get_balance())))
account.deposit(-20)
account.deposit(0)
account.withdraw(-5)
print("Balance: " + str(int(account.get_balance())))
if account.withdraw(120):
    print("Withdrew all")
print("Balance: " + str(int(account.get_balance())))
if not account.withdraw(1):
    print("Rejected")

STEP 1 OF 12
    Ctrl + Enter to check

    Bank Account in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free