Skip to content

Commit 777239b

Browse files
committed
mortgage solution
1 parent cdc4e9d commit 777239b

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

Work/mortgage.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,167 @@
11
# mortgage.py
22
#
33
# Exercise 1.7
4+
5+
print('Exercise 1.7')
6+
7+
principal = 500000.0
8+
rate = 0.05
9+
payment = 2684.11
10+
total_paid = 0.0
11+
12+
while principal > 0:
13+
principal = principal * (1 + rate / 12) - payment
14+
total_paid = total_paid + payment
15+
16+
print('Total paid', total_paid)
17+
print('\n')
18+
19+
# Exercise 1.8
20+
21+
"""
22+
Suppose Dave pays an extra $1000/month for the first 12 months of the mortgage?
23+
Modify the program to incorporate this extra payment and have it print the total amount paid along with the number of
24+
months required.
25+
When you run the new program, it should report a total payment of 929,965.62 over 342 months
26+
"""
27+
28+
print('Exercise 1.8')
29+
30+
principal = 500000.0
31+
rate = 0.05
32+
payment = 2684.11
33+
total_paid = 0.0
34+
35+
for _ in range(12):
36+
principal = principal * (1 + rate / 12) - (payment + 1000.0)
37+
total_paid = total_paid + (payment + 1000.0)
38+
39+
while principal > 0:
40+
principal = principal * (1 + rate / 12) - payment
41+
total_paid = total_paid + payment
42+
43+
print('Total paid', total_paid)
44+
print('\n')
45+
46+
# Exercise 1.9
47+
48+
"""
49+
Modify the program so that extra payment information can be more generally handled. Make it so that the user can set
50+
these variables:
51+
52+
extra_payment_start_month = 61
53+
extra_payment_end_month = 108
54+
extra_payment = 1000
55+
Make the program look at these variables and calculate the total paid appropriately.
56+
57+
How much will Dave pay if he pays an extra $1000/month for 4 years starting in year 5 of the mortgage?
58+
"""
59+
60+
print('Exercise 1.9')
61+
62+
principal = 500000.0
63+
rate = 0.05
64+
payment = 2684.11
65+
total_paid = 0.0
66+
# extra_payment_start_month = (4 * 12) + 1 # starting of year 5
67+
# extra_payment_end_month = extra_payment_start_month + (4 * 12)
68+
extra_payment_start_month = 61
69+
extra_payment_end_month = 108
70+
extra_payment = 1000
71+
72+
month = 1
73+
while principal > 0:
74+
principal = principal * (1 + rate / 12) - payment
75+
total_paid = total_paid + payment
76+
if extra_payment_start_month <= month <= extra_payment_end_month:
77+
principal = principal - extra_payment
78+
total_paid = total_paid + extra_payment
79+
80+
month += 1
81+
82+
print(f'Total paid in {month} months: {total_paid}')
83+
print('\n')
84+
85+
# Exercise 1.10
86+
87+
"""
88+
Modify the program to print out a table showing the month, total paid so far, and the remaining principal.
89+
The output should look something like this:
90+
1 2684.11 499399.22
91+
2 5368.22 498795.94
92+
3 8052.33 498190.15
93+
4 10736.44 497581.83
94+
5 13420.55 496970.98
95+
...
96+
308 874705.88 3478.83
97+
309 877389.99 809.21
98+
310 880074.1 -1871.53
99+
Total paid 880074.1
100+
Months 310
101+
"""
102+
103+
print('Exercise 1.10')
104+
105+
principal = 500000.0
106+
rate = 0.05
107+
payment = 2684.11
108+
total_paid = 0.0
109+
# extra_payment_start_month = (4 * 12) + 1 # starting of year 5
110+
# extra_payment_end_month = extra_payment_start_month + (4 * 12)
111+
extra_payment_start_month = 61
112+
extra_payment_end_month = 108
113+
extra_payment = 1000
114+
115+
month = 1
116+
while principal > 0:
117+
principal = principal * (1 + rate / 12) - payment
118+
total_paid = total_paid + payment
119+
if extra_payment_start_month <= month <= extra_payment_end_month:
120+
principal -= extra_payment
121+
total_paid += extra_payment
122+
print(f'{month} {round(total_paid, 2)} {round(principal, 2)}')
123+
month += 1
124+
125+
print('\n')
126+
127+
# Exercise 1.11
128+
129+
"""
130+
While you’re at it, fix the program to correct for the overpayment that occurs in the last month.
131+
"""
132+
133+
print('Exercise 1.11')
134+
135+
principal = 500000.0
136+
rate = 0.05
137+
payment = 2684.11
138+
total_paid = 0.0
139+
# extra_payment_start_month = (4 * 12) + 1 # starting of year 5
140+
# extra_payment_end_month = extra_payment_start_month + (4 * 12)
141+
extra_payment_start_month = 61
142+
extra_payment_end_month = 108
143+
extra_payment = 1000
144+
145+
month = 1
146+
while principal > 0:
147+
if payment >= principal:
148+
payment = principal
149+
principal -= payment
150+
total_paid += payment
151+
print(
152+
f'In month {month} principal is {round(principal, 2)} because we have paid a total of '
153+
f'{round(total_paid, 2)} with a payment of {round(payment, 2)}'
154+
)
155+
break
156+
principal = principal * (1 + rate / 12) - payment
157+
total_paid = total_paid + payment
158+
if extra_payment_start_month <= month <= extra_payment_end_month:
159+
principal -= extra_payment
160+
total_paid += extra_payment
161+
print(
162+
f'In month {month} principal is {round(principal, 2)} because we have paid a total of '
163+
f'{round(total_paid, 2)} with a payment of {round(payment, 2)}'
164+
)
165+
month += 1
166+
167+
print('\n')

0 commit comments

Comments
 (0)