Skip to the content

Project: bank account

Python · Unit 12: Classes and objects

You're going to program a bank account. It stores its owner's name and its balance, which starts at 0.

You can deposit and withdraw, but never go below zero: if there isn't enough, the withdrawal doesn't happen and the balance stays the same.

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Complete withdraw so it only takes the money out if there's enough. It should print 0.

  2. 2. Write the code

    Complete the Account class: deposit(amount) adds to the balance, and withdraw(amount) subtracts from it only if there's enough. The simulate function below is already written: it creates an account and applies the moves, where a positive number is a deposit and a negative one, a withdrawal.

  3. 3. Write the code

    Write send(other, amount): if this account can withdraw the amount, deposit it into other. If there isn't enough, neither account changes. simulate creates Ana's and Leo's accounts, makes the transfer and returns both balances.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →