Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Python bank account class with deposit, withdraw and history

A class bundles data and actions for an account, and the code raises errors for overdrawing. It introduces object oriented programming with a familiar example.

What it shows

A class bundles data and actions for an account, and the code raises errors for overdrawing. It introduces object oriented programming with a familiar example.

The science

A class is a blueprint for objects. Each object made from it has its own data, called attributes, such as the owner and balance, and its own actions, called methods, such as deposit. The __init__ method sets up a new object. Checking inside the method that a withdrawal does not exceed the balance keeps the data valid, which is called encapsulation.

Materials

Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • A computer, laptop or phone with a browser (free)
  • Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
  • Only made-up names and balances for testing
  • A notebook and a pencil

Safety

For this project: This is a practice program with made-up money. Never enter real bank details into any practice program.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Create bank.py and write class Account: followed by an indented def __init__(self, owner, balance=0): that sets self.owner = owner, self.balance = balance and self.history = [].

  2. Add def deposit(self, amount): that raises ValueError("Amount must be positive") if amount <= 0, otherwise adds it to self.balance and appends ("deposit", amount) to self.history.

  3. Add def withdraw(self, amount): that raises ValueError("Not enough balance") if amount > self.balance, otherwise subtracts it and records ("withdraw", amount).

  4. Add def __str__(self): returning self.owner + " has Rs " + str(self.balance).

  5. Add def show_history(self): that prints each pair with for kind, amt in self.history: print(kind, amt).

  6. Under the class (no indent) write a = Account("Meera", 1000), a.deposit(500), a.withdraw(300), print(a) and a.show_history().

  7. Test the errors with try: a.withdraw(5000) and except ValueError as e: print("Error:", e).

  8. Make a second account b = Account("Karan") and check that depositing into b does not change a, proving each object has its own data.

What you should see

The program prints Meera has Rs 1200, shows the history of the two transactions and prints a clear error message when a withdrawal is more than the balance.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

1What is a class?

A blueprint from which objects are created.

2What does __init__ do?

It sets up a new object with its starting values.

3Why raise an error for overdrawing?

To keep the balance valid and tell the caller what went wrong.

4Why do two accounts not affect each other?

Each object has its own attributes.

Free ebook: Viva Questions for Science Projects

Take it further

Create a SavingsAccount class that inherits from Account and adds a method to add yearly interest.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

WhatsApp us