forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmortgage.py
More file actions
34 lines (26 loc) · 957 Bytes
/
mortgage.py
File metadata and controls
34 lines (26 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# mortgage.py
#
# Exercise 1.7
principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0
months = 0
extra_payment = 1000.0
extra_payment_start_month = 61
extra_payment_end_month = 108
while principal > 0:
months += 1
principal = principal * (1+rate/12) - payment
total_paid = total_paid + payment
# Apply extra payment if within the specified months
if months >= extra_payment_start_month and months <= extra_payment_end_month:
principal = principal - extra_payment
total_paid = total_paid + extra_payment
# Ensure we don't pay more than the remaining principal
if principal < 0:
total_paid = total_paid + principal # Adjust final payment
principal = 0.0 # To avoid negative principal in final month
print(f"{months} {total_paid:.2f} {principal:.2f} ")
print(f"Total paid {total_paid:.2f}")
print(f"Months {months} or {months // 12} years and {months % 12} months")