#49 · Classes · Beginner · 45 XP · about 10 min
Build a BankAccount class.
BankAccount(balance=0, overdraft_limit=0) opens an account. balance is the starting money and overdraft_limit is how far below zero the balance may go.deposit(amount) adds money and returns the new balance.withdraw(amount) takes money out and returns True, but only if the balance would stay at or above -overdraft_limit. Otherwise it changes nothing and returns False.transfer(other, amount) moves money into the account other, following the same rule as withdraw, and returns True or False in the same way.balance.acct = BankAccount(100, overdraft_limit=50) acct.withdraw(120) # returns True, and the balance is now -20 acct.withdraw(40) # returns False: -60 would go past the limit acct.deposit(30) # returns 10 acct.balance # 10
All amounts are positive whole numbers.
Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.