Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Using the BankAccount class of the lesson, add a method transfer(other, amount) that moves money from this account to another account. If the money is not there, it must refuse with a ValueError, and neither balance may change.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Reuse withdraw and deposit, which already hold the rules. Withdraw first: if it raises, nothing has changed yet.
Check. After the first transfer: 70 and 50. The second is refused by withdraw, so neither balance changes: 70 and 50.
Answer: 70 50, then refused: not enough money, then 70 50.
Log in to join the discussion.
Loading…