Sub topic 11
Simulating both methods in code
The earlier chapters worked through the avalanche and snowball methods by hand. This chapter turns the rules into a small Python program so you can test them on your own debts. You need to read a short script and recall how monthly interest works (APR divided by 12, applied to the balance). No libraries are required.
What the simulation models
A simulation repeats one month of your debt life until every balance is zero. Each month has three steps, always in the same order:
- Interest accrues on every balance.
- The minimum payment goes to every debt.
- All remaining budget goes to one target debt, then spills to the next.
The only difference between the two methods is the sort order in step 3. Avalanche sorts by APR, highest first. Snowball sorts by balance, smallest first.
The simulation function
This is the core function. It copies the debts so your input list is never changed, and it stops with an error in two situations: the budget cannot cover the minimums, or the loop runs past 1,200 months.
def payoff(debts, budget, strategy):
debts = [dict(d) for d in debts]
if budget < sum(d['minimum'] for d in debts):
raise ValueError('budget is below the sum of minimums')
months, total_interest = 0, 0.0
while any(d['balance'] > 0 for d in debts):
months += 1
if months > 1200:
raise ValueError('not paid off within 100 years')
# 1. Accrue one month of interest on every debt.
for d in debts:
interest = round(d['balance'] * d['apr'] / 12, 2)
d['balance'] = round(d['balance'] + interest, 2)
total_interest += interest
# 2. Pay the minimum on every debt.
available = budget
for d in debts:
pay = min(d['minimum'], d['balance'])
d['balance'] = round(d['balance'] - pay, 2)
available -= pay
# 3. Send all remaining money to the target debt, then the next.
if strategy == 'avalanche':
order = sorted(debts, key=lambda d: -d['apr'])
else:
order = sorted(debts, key=lambda d: d['balance'])
for d in order:
pay = min(available, d['balance'])
d['balance'] = round(d['balance'] - pay, 2)
available -= pay
return months, round(total_interest, 2)Three details matter for correctness.
- Interest comes first. The payment in a month is applied to a balance that already includes that month's interest. This matches how most statements work.
min(..., d['balance'])caps every payment. A final payment never exceeds what is owed, and the unused money flows to the next debt in the same month.- Rollover needs no special code. Once a debt reaches zero,
min(d['minimum'], 0)is zero, so its old minimum stays inavailable. That money automatically goes to the next target. This is the rollover effect from the earlier chapter.
Running both strategies
The next script uses the same three debts and the same 500 monthly budget for both strategies. Using identical inputs is what makes the comparison fair.
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(strategy, months, interest)The minimums add up to 235, so 265 is extra each month. Run it yourself to see the exact figures. Avalanche should show total interest that is equal to or lower than snowball's. The month counts are usually close, and they can be identical.
Checking the output by hand
Do not trust a simulation until you have checked one month yourself. Here is month 1 for the debts above.
| Step | Card A | Card B | Loan |
|---|---|---|---|
| Starting balance | 3,000.00 | 800.00 | 6,000.00 |
| Interest (balance × APR ÷ 12) | 60.00 | 8.00 | 35.00 |
| After interest | 3,060.00 | 808.00 | 6,035.00 |
| After minimums (90, 25, 120) | 2,970.00 | 783.00 | 5,915.00 |
Total interest in month 1 is 103.00 for both strategies, because interest depends only on the starting balances. Then the 265 of extra money diverges:
- Avalanche sends 265 to Card A (24% APR). Balances become 2,705.00, 783.00 and 5,915.00.
- Snowball sends 265 to Card B (smallest balance). Balances become 2,970.00, 518.00 and 5,915.00.
In month 2 the difference shows up as interest. Avalanche accrues 54.10 + 7.83 + 34.50 = 96.43. Snowball accrues 59.40 + 5.18 + 34.50 = 99.08. Avalanche is already paying 2.65 less per month. Snowball's reward is that Card B disappears sooner, which reduces the number of accounts you manage.
Recording each month
A final total hides the story. To print balances over time, add an optional callback that receives the state at the end of each month. This version replaces the function above, and calling it without a callback behaves the same.
def payoff(debts, budget, strategy, on_month=None):
debts = [dict(d) for d in debts]
if budget < sum(d['minimum'] for d in debts):
raise ValueError('budget is below the sum of minimums')
months, total_interest = 0, 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 d in debts:
interest = round(d['balance'] * d['apr'] / 12, 2)
d['balance'] = round(d['balance'] + interest, 2)
total_interest += interest
available = budget
for d in debts:
pay = min(d['minimum'], d['balance'])
d['balance'] = round(d['balance'] - pay, 2)
available -= pay
if strategy == 'avalanche':
order = sorted(debts, key=lambda d: -d['apr'])
else:
order = sorted(debts, key=lambda d: d['balance'])
for d in order:
pay = min(available, d['balance'])
d['balance'] = round(d['balance'] - pay, 2)
available -= pay
if on_month:
on_month(months, {d['name']: d['balance'] for d in debts})
return months, round(total_interest, 2)
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},
]
def show(month, balances):
if month <= 3:
print(month, balances)
for strategy in ('avalanche', 'snowball'):
print(strategy)
payoff(debts, 500.0, strategy, on_month=show)The first line for each strategy should match the hand calculation above. If it does not, the bug is in the simulation or in your arithmetic, and now you can find it.
Edge cases and failure modes
Test the guards deliberately, because they protect you from silent nonsense.
single = [{'name': 'Solo', 'balance': 1000.0, 'apr': 0.18, 'minimum': 50.0}]
print(payoff(single, 200.0, 'avalanche') == payoff(single, 200.0, 'snowball'))
try:
payoff(debts, 100.0, 'avalanche')
except ValueError as err:
print(err)
stuck = [{'name': 'Stuck', 'balance': 10000.0, 'apr': 0.24, 'minimum': 150.0}]
try:
payoff(stuck, 150.0, 'snowball')
except ValueError as err:
print(err)- Single debt. Ordering has nothing to decide, so both strategies must return the same answer. The first line should print
True. - Budget below minimums. The function refuses to run. A plan that cannot cover required payments is not a plan.
- Minimum below monthly interest. The stuck debt accrues 200 of interest each month but receives only 150. The balance grows forever, and the 1,200-month guard turns an infinite loop into an error.
Other gotchas to keep in mind:
- Equal budget and minimums does not mean equal results. Even with no extra money at the start, each paid-off debt frees its minimum. That freed money is then ordered differently by the two strategies.
- Rounding. Rounding to cents each step mimics a statement, but it can move totals by a few cents. Treat differences of a few cents between strategies as noise.
- Ties. Two debts with equal APR (or equal balance) are ordered by their position in the list. Python's sort is stable, so this is predictable.
- Fixed rates only. This model assumes the APR never changes and there are no fees, promotional rates, or new charges. The complications chapter relaxes those assumptions.
Reading the results
Compare the two runs on three numbers: months to debt-free, total interest, and the month when the first debt disappears. Avalanche usually wins on total interest. Snowball wins on the first payoff date. Whether a gap of a few dozen or a few hundred dollars is worth the faster early win is a behavioral decision, which the comparison chapter covers. The simulation gives you the price of each choice.
Exercise
Using the three debts and the 500 budget from this chapter, what are the balances of Card A, Card B and the Loan at the end of month 1 under snowball? Why is total month-1 interest identical for both strategies?
Show answer
Card A is 2,970.00, Card B is 518.00 and the Loan is 5,915.00. Interest is 60.00 + 8.00 + 35.00 = 103.00 under either strategy because interest is calculated on starting balances before any payment is made. The strategies differ only in where the 265.00 of extra money goes, which first affects interest in month 2.
Exercise
A debt has a 3,000 balance, 24% APR and a 40 minimum payment, and your budget equals that 40. What happens when you run the simulation, and why?
Show answer
The function raises ValueError('not paid off within 100 years'). Monthly interest is 3,000 × 0.24 ÷ 12 = 60, which is more than the 40 payment, so the balance grows every month and never reaches zero. The 1,200-month guard stops the loop.