Sub topic 11
Simulating Both Methods in Code
This chapter assumes you understand balances, APRs, minimum payments, and basic Python. The simulation uses a fixed monthly budget and compares two payoff orderings. It is a planning model, not an exact statement calculator.
Model the monthly cycle
The simulation repeats these steps:
- Add one month of interest to every unpaid debt.
- Pay the minimum payment on every debt.
- Send the remaining budget to the target debt.
- Continue with the next target after a debt reaches zero.
APR means annual percentage rate. Dividing APR by 12 estimates one month of interest. For example, a 24% APR produces a monthly rate of 0.24 / 12, or 2%.
Write the payoff function
The strategy argument selects the target order. Avalanche sorts by the highest APR. Snowball sorts by the smallest balance. The function copies each debt first, so the original list is not changed.
def payoff(debts, budget, strategy):
debts = [dict(d) for d in debts]
minimums = sum(d["minimum"] for d in debts)
if budget < minimums:
raise ValueError("budget is below the sum of minimums")
months = 0
total_interest = 0.0
while any(d["balance"] > 0 for d in debts):
months += 1
if months > 1200:
raise ValueError("not paid off within 100 years")
for debt in debts:
interest = round(debt["balance"] * debt["apr"] / 12, 2)
debt["balance"] = round(debt["balance"] + interest, 2)
total_interest += interest
available = budget
for debt in debts:
payment = min(debt["minimum"], debt["balance"])
debt["balance"] = round(debt["balance"] - payment, 2)
available -= payment
if strategy == "avalanche":
order = sorted(debts, key=lambda d: -d["apr"])
elif strategy == "snowball":
order = sorted(debts, key=lambda d: d["balance"])
else:
raise ValueError("strategy must be avalanche or snowball")
for debt in order:
payment = min(available, debt["balance"])
debt["balance"] = round(debt["balance"] - payment, 2)
available -= payment
return months, round(total_interest, 2)When a debt is paid off, its minimum payment is no longer needed. Because that amount remains in available, it automatically rolls into the next target. This is the rollover effect.
Compare the strategies
Use the same debts and budget for both runs. Otherwise, the comparison is not meaningful.
debts = [
{"name": "Card A", "balance": 3000.0, "apr": 0.24, "minimum": 90.0},
{"name": "Card B", "balance": 800.0, "apr": 0.12, "minimum": 25.0},
{"name": "Loan", "balance": 6000.0, "apr": 0.07, "minimum": 120.0},
]
for strategy in ("avalanche", "snowball"):
months, interest = payoff(debts, 500.0, strategy)
print(f"{strategy}: {months} months, ${interest:.2f} interest")| Result | What it measures |
|---|---|
| Months | How long the debts take to reach zero |
| Interest | The modeled interest paid during that time |
| Strategy | The ordering used for extra payments |
The avalanche usually reduces interest when rates differ. The snowball may eliminate a small account sooner, which can make the plan easier to maintain. Compare both interest and time, then include your ability to follow the plan.
Exercise
If the total minimum payments are $235 and your monthly debt budget is $500, how much extra money is available for the first target debt?
Show answer
$265. The available amount is $500 - $235.