Exercise · Classes · lesson 10 of 11
Exercise: bank account
Write a class BankAccount, a bank account:
- the call
BankAccount(owner, balance=0)stores the owner's name in the attributeownerand the starting balance in the attributebalance; - the method
deposit(self, amount)addsamountto the balance; - the method
withdraw(self, amount)takesamountout of the account and returnsTrue; if the account holds less thanamount, it returnsFalseand leaves the balance unchanged.
Examples:
BankAccount("Ann").balance→0acc = BankAccount("Ann", 50), thenacc.withdraw(20)→True, andacc.balance→30- after that,
acc.withdraw(40)→False, andacc.balanceis still30
Constraints: amounts are non-negative integers; withdrawing exactly the whole balance works; each account has its own balance.
In the tests, getattr(acc, "balance", None) reads acc.balance, or gives None if there is no such attribute.
Do not add extra imports unless the task says so explicitly.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.