so i have this program that calculate that calculates the total amount that Dave will have to pay over the life of the mortgage:
# mortgage.py
principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0
while principal > 0:
principal = principal * (1 rate/12) - payment
total_paid = total_paid payment
print('Total paid', total_paid)
and later the exercise ask me Suppose Dave pays an extra $1000/month for the first 12 months of the mortgage?
Modify the program to incorporate this extra payment and have it print the total amount paid along with the number of months required. what can i modify to make the changes to the program ? I am lost
CodePudding user response:
Here you need to ask again to the user "Do Dave has paid anything extra in principal amount?" if Yes then create new variable for updated principal update_principal = principal dave_paid_extra and then re run the code with same eqaution.
CodePudding user response:
principal=500000.0
rate= 0.0
payment = 2684.11
total_paid = 0.0
extra_payment = 1000.0
num_periods = 0
while principal > 0:
if num_periods < 12:
principal = principal * (1 rate/12) - (payment extra_payment)
total_paid = payment extra_payment
else:
principal = principal * (1 rate/12) - (payment)
total_paid = payment
num_periods = 1
print('Total paid: $', total_paid)
Total paid = 929965.6199999959, but principal = -1973.205724763917
You have overpayed the final payment remember to add 'principal' to 'total_paid' to get the actual amount: 927992.414275232
Also, the numbers are not rounded to 2 decimal places you can round them by using:
def twoDecimalPlaces(answer):
return("%.2f" % answer)
twoDecimalPlaces(927992.414275232)
>>> 927992.41