@@ -16,7 +16,14 @@ def level_payment(principal, annual_rate_pct, years, payments_per_year=12):
1616 return principal * (r * factor ) / (factor - 1 )
1717
1818
19- def amortization_schedule (principal , annual_rate_pct , years , payments_per_year = 12 , print_annual_summary = False , eps = 1e-9 ):
19+ def amortization_schedule (
20+ principal ,
21+ annual_rate_pct ,
22+ years ,
23+ payments_per_year = 12 ,
24+ print_annual_summary = False ,
25+ eps = 1e-9 ,
26+ ):
2027 pmt = level_payment (principal , annual_rate_pct , years , payments_per_year )
2128 r = (annual_rate_pct / 100.0 ) / payments_per_year
2229 n = years * payments_per_year
@@ -32,7 +39,6 @@ def amortization_schedule(principal, annual_rate_pct, years, payments_per_year=1
3239 )
3340 )
3441
35-
3642 for period in range (1 , n + 1 ):
3743 interest = balance * r
3844 principal_component = pmt - interest
@@ -44,23 +50,25 @@ def amortization_schedule(principal, annual_rate_pct, years, payments_per_year=1
4450 else :
4551 payment_made = pmt
4652
47- if principal_component < 0 and principal_component > - eps : # clamp tiny negatives
53+ if (
54+ principal_component < 0 and principal_component > - eps
55+ ): # clamp tiny negatives
4856 principal_component = 0.0
4957
5058 balance = max (0.0 , balance - principal_component )
5159 schedule .append ([period , payment_made , interest , principal_component , balance ])
5260
5361 # streamline for all time periods (monthly/quarterly/biweekly/weekly)
54- months_elapsed = ( round ((period * 12 ) / payments_per_year ) )
62+ months_elapsed = round ((period * 12 ) / payments_per_year )
5563
5664 if print_annual_summary and (months_elapsed % 12 == 0 or balance <= eps ):
5765 tenure_left_periods = n - period
5866 print (
59- (
60- f"{ months_elapsed // 12 :<6} { months_elapsed :<12} { tenure_left_periods :<13} "
61- f"{ pmt :<16.2f} { balance :<14.2f} "
67+ (
68+ f"{ months_elapsed // 12 :<6} { months_elapsed :<12} { tenure_left_periods :<13} "
69+ f"{ pmt :<16.2f} { balance :<14.2f} "
70+ )
6271 )
63- )
6472
6573 if balance <= eps :
6674 break
@@ -72,5 +80,7 @@ def amortization_schedule(principal, annual_rate_pct, years, payments_per_year=1
7280 return round (pmt , 4 ), schedule
7381
7482
75- pmt , sched = amortization_schedule (10000 , 5.5 , 15 , payments_per_year = 12 , print_annual_summary = True )
83+ pmt , sched = amortization_schedule (
84+ 10000 , 5.5 , 15 , payments_per_year = 12 , print_annual_summary = True
85+ )
7686print (pmt )
0 commit comments