Objective
A class protects its data: the balance only changes through methods that check that the operation is valid and, if it is not, warn with an exception.
Instructions
- Define the exception
InsufficientBalanceand the classAccount, with the balance in a private attribute_balance(it starts at 0), a read-only propertybalanceand a counteroperationsof operations carried out. - The method
deposit_money(amount)adds the amount andwithdraw_money(amount)subtracts it. If the amount is not greater than 0, they raiseValueErrorwith the messageinvalid amount; if more is withdrawn than there is,InsufficientBalancewithinsufficient balance. - Read
nand thennlines withdepositorwithdraw, and the quantity. After each successful operation showBalance: x; if it fails, showError: messagewith the exception message. - At the end show
Final balance: xandOperations: m(only successful operations count).
4
deposit 100
withdraw 30
withdraw 100
deposit 0
Balance: 100
Balance: 70
Error: insufficient balance
Error: invalid amount
Final balance: 70
Operations: 2
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise