Exercise 2: Adding __sub__ to Money — Possible Solution ==================================================================== THE NEW METHOD ------------------------------ def __sub__(self, other): if self.currency != other.currency: raise ValueError(f'Cannot subtract {self.currency} and {other.currency}') return Money(self.amount - other.amount, self.currency) Follows this chapter's own __add__ exactly - the identical currency check, the only difference being subtraction instead of addition and an error message worded for subtraction specifically. VERIFYING SAME-CURRENCY SUBTRACTION WORKS CORRECTLY ------------------------------ same-currency subtraction: 15.0 USD Money(25.0, 'USD') - Money(10.0, 'USD') correctly returns 15.0 USD. VERIFYING CROSS-CURRENCY SUBTRACTION IS CORRECTLY REJECTED ------------------------------ cross-currency subtraction correctly rejected: Cannot subtract USD and EUR Money(25.0, 'USD') - Money(10.0, 'EUR') correctly raises ValueError, exactly mirroring this chapter's own __add__ finding for the identical kind of mismatched-currency mistake. WHY THIS CONFIRMS THE PATTERN GENERALIZES BEYOND ADDITION ------------------------------ This chapter's own Money example only verified the invariant (never combine two different currencies) for addition specifically. This exercise confirms the SAME invariant, enforced the SAME way, correctly applies to a second operation without needing any new validation logic invented from scratch - the currency check itself is the reusable part; only the arithmetic operation performed after the check changes. WHY THIS WORKS AS AN ANSWER ------------------------------ The new method mirrors this chapter's own __add__ exactly in structure, and both the correct same-currency case and the correctly-rejected cross-currency case are verified directly, confirming the Money type's own core guarantee (never silently combine two currencies) holds for a second operation, not just the one originally demonstrated.