DEV Community

Cover image for Mortgage Calculator Python
GenIl
GenIl

Posted on

Mortgage Calculator Python

I've done this portfolio project in Computer Science course CodeAcademy

Project Objectives:

  • Build a terminal program using Python
  • Add at least one interactive feature using input()
  • Use Git version control
  • Use the command line and file navigation
  • Write a technical blog post on the project

I've chosen to build Mortgage Calculator. It's not so fun but pretty useful.

Functionality of the terminal program

Take user's input:

  • Duration of loan programm
  • Loan Amount
  • Interest Rate per year

Mortgage calculation Implementation

Output:

  • Total Monthly Payment
  • Total Interest Paid
  • Total of n-months Payments
  • Show monthly amortization table.

Work on the project consists of three parts:

  1. Project Brainstorming
  2. Exploring subject area
  3. Program implementation

The project have been implemented in two versions. With python packages and without it.

In the first version I use numpy_financial 1.0.0 package. It contains a collection of elementary financial functions. In particular pmt() function that computes the payment against loan principal plus interest (a monthly mortgage payment). As well as ipmt() and ppmt(). The interest portion of monthly payment and the payment against loan principal. This two functions return an array of Interest/principal portion of payment for every month.

The total payment is made up of payment against principal plus interest.

pmt = ppmt + ipmt

The second version uses mortgage formula for calculating monthly payment.

Code consists three parts:

  • Variables-Input part
  • Functions part
  • Output part

In Functions part we have paidEveryMonth(interestRate, months, borrowed):

def paidEveryMonth(interestRate, months, borrowed):             #fixed monthly payment
    return (borrowed * true_rate * ((true_rate + 1)**months)) / ((true_rate + 1)**months - 1)#Mortgage formula
Enter fullscreen mode Exit fullscreen mode

There is the logic of the Mortgage function:

Alt Text

def fullReport(total_pem, borrowed) function forms monthly amortization table.

def fullReport(total_pem, borrowed):
    print("||||||||||||||||||||||||||||||||||||||||")
    print("MONTH -> P&I   " + "= PRINCIPAL + INTEREST| PRINCIPAL REMAINING")
    fmt = '{0:2d} -> {4:8.2f} = {1:8.2f} + {2:8.2f} | {3:8.2f}' # Format our table
    for index in range(1, months + 1):                          #for every month
        interest = borrowed * interestRate / 12                 #interest to the end of the month
        principal = total_pem - interest                        #principal repayment
        borrowed -= principal                                   #balance      
        print(fmt.format(index, principal, interest, borrowed, total_pem))
Enter fullscreen mode Exit fullscreen mode

A link to my code on GitHub.

Top comments (1)

Collapse
 
pinishbreeding profile image
Pinishbreeding • Edited

"I am currently learning python through a youtube video tutorial and have come up with a formula that I don't seem to understand as nothing fits me. The basic concept of the exercise is to make a mortgage calculator that asks the user to enter 3 pieces of information, loan amount, interest rate, and loan term (years). It calculates the monthly payments to the user.
My Mortgage Broker Cheltenham, inspired me to create this tool. I'm new to python, so carry me. The teachers in the video used python3, and I use python2, so I'm not sure if all the syntaxes are the same."

Some comments have been hidden by the post's author - find out more