In: Computer Science
Python:
Implement a function called monthlyAmortization() that takes the following parameters:
the principal amount of the loan (p)
the interest rate, or annual rate (r)
the term of the loan in months (t)
the payment amount each period (monthlyPayment)
The function prints the amortization table to a file called amoritizedLoan.txt.
At the end of this file, the fucntion prints the total amount of interest paid for the loan.
The output needs to be nicely formatted as shown below.
The example has the following parameters: for p = 100,000; r = 6% yearly; monthlyPayment = 599.55; t = 30 years, or 360 months:
>>> monthlyAmortization(100000, .06, 360, 599.55)
>>>
The output file amortizedLoan.txt:
Your amortized loan of $100,000.00 for 360 months at 6.0% annual interest rate.
Month Principal Interest Payment Principal Payment Ending Principal
1 $100,000.00 $500.00 $99.55 $99,900.45
2 $99,900.45 $499.50 $100.05 $99,800.40
3 $99,800.40 $499.00 $100.55 $99,699.85
………………………………………………………………..
358 $1,781.33 $8.91 $590.64 $1,190.69
359 $1,190.69 $5.95 $593.60 $597.09
360 $597.09 $2.99 $596.56 $0.53
The total amount of interest paid: $115,838.53
-------
The following formulas are helpful:
Remember to convert the annually interest rate to the monthly interest rate: .06/12
Month 1
monthlyInterestPayment = p*r
principalPayment = ($599.55 payment - $500 interest = $99.55 principal payment).
endingPrinciple = principle – principalPayment = 100,000 – 99.55 = 99,900.45
Month 2
principal = last month’s endingPrincipal = 99,900.45
…..
This example was taken from How to Calculate Amortization, October 1, 2019,
https://www.vertex42.com/ExcelArticles/amortization-calculation.html
def monthlyAmortization(p, r, months, mp):
# opening the file and writing the header
f = open("amoritizedLoan.txt", "w")
f.write("Month Principal Interest Payment Principal Payment Ending Principal\n")
r = r / 12
# looping for months number of times
for i in range(months):
# updating the values
pp = mp - p*r
ep = p - pp
# writing to file
f.write(str(i+1)+ "\t"+ str(round(p,2))+str(round( p*r,2)) + "\t"+ str(round(pp,2))+"\t"+ str(round(ep,2)) + "\n")
p= ep
# done with writing, closing the file
f.close()
monthlyAmortization(100000, .06, 360, 599.55)
''' OUTPUT file:
Month Principal Interest Payment Principal Payment Ending
Principal
1 100000500.0 99.55
99900.45
2 99900.45499.5 100.05
99800.4
3 99800.4499.0 100.55
99699.85
4 99699.85498.5 101.05
99598.8
5 99598.8497.99 101.56
99497.25
6 99497.25497.49 102.06
99395.18
7 99395.18496.98 102.57
99292.61
8 99292.61496.46 103.09
99189.52
9 99189.52495.95 103.6
99085.92
10 99085.92495.43 104.12
98981.8
11 98981.8494.91 104.64
98877.16
12 98877.16494.39 105.16
98771.99
13 98771.99493.86 105.69
98666.3
14 98666.3493.33 106.22
98560.09
15 98560.09492.8 106.75
98453.34
16 98453.34492.27 107.28
98346.05
17 98346.05491.73 107.82
98238.23
18 98238.23491.19 108.36
98129.87
19 98129.87490.65 108.9
98020.97
20 98020.97490.1 109.45
97911.53
21 97911.53489.56 109.99
97801.54
22 97801.54489.01 110.54
97690.99
23 97690.99488.45 111.1
97579.9
24 97579.9487.9 111.65
97468.25
25 97468.25487.34 112.21
97356.04
26 97356.04486.78 112.77
97243.27
27 97243.27486.22 113.33
97129.94
28 97129.94485.65 113.9
97016.04
29 97016.04485.08 114.47
96901.57
30 96901.57484.51 115.04
96786.52
31 96786.52483.93 115.62
96670.91
32 96670.91483.35 116.2
96554.71
33 96554.71482.77 116.78
96437.94
34 96437.94482.19 117.36
96320.57
35 96320.57481.6 117.95
96202.63
36 96202.63481.01 118.54
96084.09
37 96084.09480.42 119.13
95964.96
38 95964.96479.82 119.73
95845.24
39 95845.24479.23 120.32
95724.91
40 95724.91478.62 120.93
95603.99
41 95603.99478.02 121.53
95482.46
42 95482.46477.41 122.14
95360.32
43 95360.32476.8 122.75
95237.57
44 95237.57476.19 123.36
95114.21
45 95114.21475.57 123.98
94990.23
46 94990.23474.95 124.6
94865.63
47 94865.63474.33 125.22
94740.41
48 94740.41473.7 125.85
94614.56
49 94614.56473.07 126.48
94488.08
50 94488.08472.44 127.11
94360.97
51 94360.97471.8 127.75
94233.23
52 94233.23471.17 128.38
94104.85
53 94104.85470.52 129.03
93975.82
54 93975.82469.88 129.67
93846.15
55 93846.15469.23 130.32
93715.83
56 93715.83468.58 130.97
93584.86
57 93584.86467.92 131.63
93453.23
58 93453.23467.27 132.28
93320.95
59 93320.95466.6 132.95
93188.0
60 93188.0465.94 133.61
93054.39
61 93054.39465.27 134.28
92920.12
62 92920.12464.6 134.95
92785.17
63 92785.17463.93 135.62
92649.54
64 92649.54463.25 136.3
92513.24
65 92513.24462.57 136.98
92376.26
66 92376.26461.88 137.67
92238.59
67 92238.59461.19 138.36
92100.23
68 92100.23460.5 139.05
91961.18
69 91961.18459.81 139.74
91821.44
70 91821.44459.11 140.44
91680.99
71 91680.99458.4 141.15
91539.85
72 91539.85457.7 141.85
91398.0
73 91398.0456.99 142.56
91255.44
74 91255.44456.28 143.27
91112.17
75 91112.17455.56 143.99
90968.18
76 90968.18454.84 144.71
90823.47
77 90823.47454.12 145.43
90678.03
78 90678.03453.39 146.16
90531.87
79 90531.87452.66 146.89
90384.98
80 90384.98451.92 147.63
90237.36
81 90237.36451.19 148.36
90089.0
82 90089.0450.44 149.11
89939.89
83 89939.89449.7 149.85
89790.04
84 89790.04448.95 150.6
89639.44
85 89639.44448.2 151.35
89488.09
86 89488.09447.44 152.11
89335.98
87 89335.98446.68 152.87
89183.11
88 89183.11445.92 153.63
89029.47
89 89029.47445.15 154.4
88875.07
90 88875.07444.38 155.17
88719.9
91 88719.9443.6 155.95
88563.95
92 88563.95442.82 156.73
88407.22
93 88407.22442.04 157.51
88249.7
94 88249.7441.25 158.3
88091.4
95 88091.4440.46 159.09
87932.31
96 87932.31439.66 159.89
87772.42
97 87772.42438.86 160.69
87611.73
98 87611.73438.06 161.49
87450.24
99 87450.24437.25 162.3
87287.94
100 87287.94436.44 163.11
87124.83
101 87124.83435.62 163.93
86960.9
102 86960.9434.8 164.75
86796.16
103 86796.16433.98 165.57
86630.59
104 86630.59433.15 166.4
86464.19
105 86464.19432.32 167.23
86296.96
106 86296.96431.48 168.07
86128.9
107 86128.9430.64 168.91
85959.99
108 85959.99429.8 169.75
85790.24
109 85790.24428.95 170.6
85619.64
110 85619.64428.1 171.45
85448.19
111 85448.19427.24 172.31
85275.88
112 85275.88426.38 173.17
85102.71
113 85102.71425.51 174.04
84928.68
114 84928.68424.64 174.91
84753.77
115 84753.77423.77 175.78
84577.99
116 84577.99422.89 176.66
84401.33
117 84401.33422.01 177.54
84223.79
118 84223.79421.12 178.43
84045.35
119 84045.35420.23 179.32
83866.03
120 83866.03419.33 180.22
83685.81
121 83685.81418.43 181.12
83504.69
122 83504.69417.52 182.03
83322.66
123 83322.66416.61 182.94
83139.73
124 83139.73415.7 183.85
82955.88
125 82955.88414.78 184.77
82771.1
126 82771.1413.86 185.69
82585.41
127 82585.41412.93 186.62
82398.79
128 82398.79411.99 187.56
82211.23
129 82211.23411.06 188.49
82022.74
130 82022.74410.11 189.44
81833.3
131 81833.3409.17 190.38
81642.92
132 81642.92408.21 191.34
81451.58
133 81451.58407.26 192.29
81259.29
134 81259.29406.3 193.25
81066.04
135 81066.04405.33 194.22
80871.82
136 80871.82404.36 195.19
80676.63
137 80676.63403.38 196.17
80480.46
138 80480.46402.4 197.15
80283.31
139 80283.31401.42 198.13
80085.18
140 80085.18400.43 199.12
79886.05
141 79886.05399.43 200.12
79685.93
142 79685.93398.43 201.12
79484.81
143 79484.81397.42 202.13
79282.69
144 79282.69396.41 203.14
79079.55
145 79079.55395.4 204.15
78875.4
146 78875.4394.38 205.17
78670.23
147 78670.23393.35 206.2
78464.03
148 78464.03392.32 207.23
78256.8
149 78256.8391.28 208.27
78048.53
150 78048.53390.24 209.31
77839.22
151 77839.22389.2 210.35
77628.87
152 77628.87388.14 211.41
77417.46
153 77417.46387.09 212.46
77205.0
154 77205.0386.03 213.52
76991.48
155 76991.48384.96 214.59
76776.88
156 76776.88383.88 215.67
76561.22
157 76561.22382.81 216.74
76344.47
158 76344.47381.72 217.83
76126.65
159 76126.65380.63 218.92
75907.73
160 75907.73379.54 220.01
75687.72
161 75687.72378.44 221.11
75466.61
162 75466.61377.33 222.22
75244.39
163 75244.39376.22 223.33
75021.06
164 75021.06375.11 224.44
74796.62
165 74796.62373.98 225.57
74571.05
166 74571.05372.86 226.69
74344.36
167 74344.36371.72 227.83
74116.53
168 74116.53370.58 228.97
73887.56
169 73887.56369.44 230.11
73657.45
170 73657.45368.29 231.26
73426.19
171 73426.19367.13 232.42
73193.77
172 73193.77365.97 233.58
72960.19
173 72960.19364.8 234.75
72725.44
174 72725.44363.63 235.92
72489.51
175 72489.51362.45 237.1
72252.41
176 72252.41361.26 238.29
72014.12
177 72014.12360.07 239.48
71774.64
178 71774.64358.87 240.68
71533.97
179 71533.97357.67 241.88
71292.09
180 71292.09356.46 243.09
71049.0
181 71049.0355.24 244.31
70804.69
182 70804.69354.02 245.53
70559.17
183 70559.17352.8 246.75
70312.41
184 70312.41351.56 247.99
70064.42
185 70064.42350.32 249.23
69815.2
186 69815.2349.08 250.47
69564.72
187 69564.72347.82 251.73
69313.0
188 69313.0346.56 252.99
69060.01
189 69060.01345.3 254.25
68805.76
190 68805.76344.03 255.52
68550.24
191 68550.24342.75 256.8
68293.44
192 68293.44341.47 258.08
68035.36
193 68035.36340.18 259.37
67775.98
194 67775.98338.88 260.67
67515.31
195 67515.31337.58 261.97
67253.34
196 67253.34336.27 263.28
66990.06
197 66990.06334.95 264.6
66725.46
198 66725.46333.63 265.92
66459.53
199 66459.53332.3 267.25
66192.28
200 66192.28330.96 268.59
65923.69
201 65923.69329.62 269.93
65653.76
202 65653.76328.27 271.28
65382.48
203 65382.48326.91 272.64
65109.84
204 65109.84325.55 274.0
64835.84
205 64835.84324.18 275.37
64560.47
206 64560.47322.8 276.75
64283.72
207 64283.72321.42 278.13
64005.59
208 64005.59320.03 279.52
63726.07
209 63726.07318.63 280.92
63445.15
210 63445.15317.23 282.32
63162.83
211 63162.83315.81 283.74
62879.09
212 62879.09314.4 285.15
62593.94
213 62593.94312.97 286.58
62307.36
214 62307.36311.54 288.01
62019.34
215 62019.34310.1 289.45
61729.89
216 61729.89308.65 290.9
61438.99
217 61438.99307.19 292.36
61146.63
218 61146.63305.73 293.82
60852.82
219 60852.82304.26 295.29
60557.53
220 60557.53302.79 296.76
60260.77
221 60260.77301.3 298.25
59962.52
222 59962.52299.81 299.74
59662.79
223 59662.79298.31 301.24
59361.55
224 59361.55296.81 302.74
59058.81
225 59058.81295.29 304.26
58754.55
226 58754.55293.77 305.78
58448.77
227 58448.77292.24 307.31
58141.47
228 58141.47290.71 308.84
57832.63
229 57832.63289.16 310.39
57522.24
230 57522.24287.61 311.94
57210.3
231 57210.3286.05 313.5
56896.8
232 56896.8284.48 315.07
56581.74
233 56581.74282.91 316.64
56265.09
234 56265.09281.33 318.22
55946.87
235 55946.87279.73 319.82
55627.05
236 55627.05278.14 321.41
55305.64
237 55305.64276.53 323.02
54982.62
238 54982.62274.91 324.64
54657.98
239 54657.98273.29 326.26
54331.72
240 54331.72271.66 327.89
54003.83
241 54003.83270.02 329.53
53674.3
242 53674.3268.37 331.18
53343.12
243 53343.12266.72 332.83
53010.29
244 53010.29265.05 334.5
52675.79
245 52675.79263.38 336.17
52339.62
246 52339.62261.7 337.85
52001.76
247 52001.76260.01 339.54
51662.22
248 51662.22258.31 341.24
51320.98
249 51320.98256.6 342.95
50978.04
250 50978.04254.89 344.66
50633.38
251 50633.38253.17 346.38
50287.0
252 50287.0251.43 348.12
49938.88
253 49938.88249.69 349.86
49589.02
254 49589.02247.95 351.6
49237.42
255 49237.42246.19 353.36
48884.06
256 48884.06244.42 355.13
48528.93
257 48528.93242.64 356.91
48172.02
258 48172.02240.86 358.69
47813.33
259 47813.33239.07 360.48
47452.85
260 47452.85237.26 362.29
47090.56
261 47090.56235.45 364.1
46726.47
262 46726.47233.63 365.92
46360.55
263 46360.55231.8 367.75
45992.8
264 45992.8229.96 369.59
45623.21
265 45623.21228.12 371.43
45251.78
266 45251.78226.26 373.29
44878.49
267 44878.49224.39 375.16
44503.33
268 44503.33222.52 377.03
44126.3
269 44126.3220.63 378.92
43747.38
270 43747.38218.74 380.81
43366.57
271 43366.57216.83 382.72
42983.85
272 42983.85214.92 384.63
42599.22
273 42599.22213.0 386.55
42212.67
274 42212.67211.06 388.49
41824.18
275 41824.18209.12 390.43
41433.75
276 41433.75207.17 392.38
41041.37
277 41041.37205.21 394.34
40647.03
278 40647.03203.24 396.31
40250.71
279 40250.71201.25 398.3
39852.41
280 39852.41199.26 400.29
39452.13
281 39452.13197.26 402.29
39049.84
282 39049.84195.25 404.3
38645.54
283 38645.54193.23 406.32
38239.21
284 38239.21191.2 408.35
37830.86
285 37830.86189.15 410.4
37420.46
286 37420.46187.1 412.45
37008.02
287 37008.02185.04 414.51
36593.51
288 36593.51182.97 416.58
36176.92
289 36176.92180.88 418.67
35758.26
290 35758.26178.79 420.76
35337.5
291 35337.5176.69 422.86
34914.64
292 34914.64174.57 424.98
34489.66
293 34489.66172.45 427.1
34062.56
294 34062.56170.31 429.24
33633.32
295 33633.32168.17 431.38
33201.94
296 33201.94166.01 433.54
32768.4
297 32768.4163.84 435.71
32332.69
298 32332.69161.66 437.89
31894.8
299 31894.8159.47 440.08
31454.73
300 31454.73157.27 442.28
31012.45
301 31012.45155.06 444.49
30567.96
302 30567.96152.84 446.71
30121.25
303 30121.25150.61 448.94
29672.31
304 29672.31148.36 451.19
29221.12
305 29221.12146.11 453.44
28767.68
306 28767.68143.84 455.71
28311.96
307 28311.96141.56 457.99
27853.97
308 27853.97139.27 460.28
27393.69
309 27393.69136.97 462.58
26931.11
310 26931.11134.66 464.89
26466.22
311 26466.22132.33 467.22
25999.0
312 25999.0129.99 469.56
25529.44
313 25529.44127.65 471.9
25057.54
314 25057.54125.29 474.26
24583.28
315 24583.28122.92 476.63
24106.65
316 24106.65120.53 479.02
23627.63
317 23627.63118.14 481.41
23146.22
318 23146.22115.73 483.82
22662.4
319 22662.4113.31 486.24
22176.16
320 22176.16110.88 488.67
21687.49
321 21687.49108.44 491.11
21196.38
322 21196.38105.98 493.57
20702.81
323 20702.81103.51 496.04
20206.77
324 20206.77101.03 498.52
19708.26
325 19708.2698.54 501.01
19207.25
326 19207.2596.04 503.51
18703.74
327 18703.7493.52 506.03
18197.7
328 18197.790.99 508.56
17689.14
329 17689.1488.45 511.1
17178.04
330 17178.0485.89 513.66
16664.38
331 16664.3883.32 516.23
16148.15
332 16148.1580.74 518.81
15629.34
333 15629.3478.15 521.4
15107.94
334 15107.9475.54 524.01
14583.93
335 14583.9372.92 526.63
14057.3
336 14057.370.29 529.26
13528.03
337 13528.0367.64 531.91
12996.12
338 12996.1264.98 534.57
12461.56
339 12461.5662.31 537.24
11924.31
340 11924.3159.62 539.93
11384.38
341 11384.3856.92 542.63
10841.76
342 10841.7654.21 545.34
10296.42
343 10296.4251.48 548.07
9748.35
344 9748.3548.74 550.81
9197.54
345 9197.5445.99 553.56
8643.98
346 8643.9843.22 556.33
8087.65
347 8087.6540.44 559.11
7528.53
348 7528.5337.64 561.91
6966.63
349 6966.6334.83 564.72
6401.91
350 6401.9132.01 567.54
5834.37
351 5834.3729.17 570.38
5263.99
352 5263.9926.32 573.23
4690.76
353 4690.7623.45 576.1
4114.67
354 4114.6720.57 578.98
3535.69
355 3535.6917.68 581.87
2953.82
356 2953.8214.77 584.78
2369.04
357 2369.0411.85 587.7
1781.33
358 1781.338.91 590.64
1190.69
359 1190.695.95 593.6
597.09
360 597.092.99 596.56
0.53
'''