Exercise 1.3

Objectives:

  • More practice at creating, saving, and running programs.

  • Looping, conditionals, and simple printing

  • Inspecting variables after program execution.

Files Created: mortgage.py

(a) Dave’s Mortgage

Dave has decided to take out a $500,000 mortgage with Guido’s Mortgage, Stock Investment, and Viagra trading corporation. Unbelievably, he got an interest rate of only 4% and an initial monthly payment of $499. However, Guido, being somewhat sneaky didn’t tell Dave that after 2 years, the interest rate would change to 9% and the monthly payment would become $3999. Write a Python program that figures out how many months it will take Dave to pay off this loan and how much it will actually cost.

To solve this problem, start by setting variables for the principal,rate, and payment:

principal = 500000.0     # Mortgage amount
rate = 0.04              # Interest rate
payment = 499            # Monthly payment

After each month, the principal will be adjusted by the following amount:

principal = principal*(1+rate/12) - payment

At 24 months, you’ll need to change the rate and payment. The output should look something like this:

Total months 677
Total paid 2623323.0

(b) Make a table

Modify the program to print out a table showing the month, total paid so far, and the remaining principal. The output should look something like this:

1 499.0 501167.666667
2 998.0 502339.225556
3 1497.0 503514.689641
4 1996.0 504694.07194
5 2495.0 505877.385513
...
675 2615325.0 5167.59241717
676 2619324.0 1207.3493603
677 2623323.0 -2782.5955195
Total months 677
Total paid 2623323.0
Links