Exercise 1.3 - Solution

(a) Dave’s Mortgage

principal  = 500000.00
rate       = 0.04
payment    = 499.00
months     = 0
total_paid = 0.00

while principal > 0:
      principal = principal*(1+rate/12) - payment
      total_paid += payment
      months += 1
      if months == 24:
           payment = 3999.00
           rate    = 0.09

print "Total months", months
print "Total paid", total_paid

(b) Make a table

principal  = 500000.00
rate       = 0.04
payment    = 499.00
months     = 0
total_paid = 0.00

while principal > 0:
      principal = principal*(1+rate/12) - payment
      total_paid += payment
      months += 1
      if months == 24:
           payment = 3999.00
           rate    = 0.09
      print months, total_paid, principal

print "Total months", months
print "Total paid", total_paid

Commentary:

After your program has finished executing, notice how all of the variables defined in your program are still alive in the interpreter. For instance, you can calculate the number of years by typing this statement:

>>> months/12.0
56.416666666666664
>>>
Tip

If you are running Python from a shell such as the Unix shell, use python -i _scriptname.py_ to have the interpreter enter interactive mode after program execution.

[ Back ]