This is a mortgage calculator I made. I took the user’s mortgage cost, time, rate, and interest to determine the rate (monthly or yearly) payment. It also prints the final info nicely including duration, total payment, total interest, total principal, and rate payment. It asks the user if they want an Amortization Table and prints one if they do. If the user pays monthly, it will total everything after each year and print it each time. It will also let the user specify what month they start, and include each month in your time period so they know what month they are in. I have already written posts about the project if you want to read about my project, the source code, and the program being run.

For my final programming project, I didn’t have to implement many new things in order to complete my project. All of the code was pretty much accomplished in the shell, and that is where everything printed out. I only had to learn one thing, which was the PrettyTable module. I needed to download this on my laptop in order to use it in my program. This module gave me a code and system so I could create the Mortgage Amortization Table. I wanted to use it so I could print all the information in a nice table and give the user everything they needed the whole length of their loan.

I did have to learn quite a bit about mortgages and how to calculate the periodic payments as well as the interest. I didn’t know how it all worked so I needed to research quite a bit to understand it. I found the formula but needed to understand what it was doing. For the interest, there were many different formulas for different interests so I had to find one at a fixed rate and use that specific one. After that, I pretty much figured out how to calculate the principal and interest for each payment on my own. The math became simpler and when I used a for loop, the code became simpler too.

 

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
if rate == "Monthly":
        monthList, monthIndex = get_month()
    #
    # Engages the pretty Table module
    #
    x = PrettyTable()
    #
    # Creates headers for the table
    #
    x.field_names = [f"Year", f"{rate} Payment", "Money to Principal", "Money to Interest", "Remaining Balance"]
    principal = ratePayment-periodInterest
    remainingBalance = amount-principal
    number = 1
    if rate == "Monthly":
        totalRatePayment = 0
        totalPeriodInterest = 0
        totalPrincipal =  0
        #
        # This prints table for however long their duration it was.
        #
        for outerCounter in range(1, int(((numberOfPayments/12)+1))):
            totalRatePayment = 0
            totalPeriodInterest = 0
            totalPrincipal =  0
            #
            # This creates a table for every year, and totals everything at the end of one year.
            #
            for innerCounter in range(1, 13):
                #
                # This adds a row under the header.
                #
                x.add_row([f"{monthList[monthIndex]}", f"{money(ratePayment)}", f"{money(principal)}", f"{money(periodInterest)}", f"{money(remainingBalance)}"])
                periodInterest = interestRate * remainingBalance
                principal = ratePayment-periodInterest
                remainingBalance = remainingBalance-principal
                totalRatePayment += ratePayment
                totalPeriodInterest += periodInterest
                totalPrincipal += principal
                monthIndex += 1
                #
                # This starts the month cycle over if it gets to December.
                #
                if monthIndex == 12:
                    monthIndex = 0
                number += 1
            x.add_row([f"Totals", f"{money(totalRatePayment)}", f"{money(totalPrincipal)}", f"{money(totalPeriodInterest)}", f"{money(remainingBalance+principal)}"])
            print(x)
            #
            # This deletes all the rows so it moves on to the next year with new information.
            #
            counter = 0
            while counter < 13:
                x.del_row(0)
                counter += 1

This is a part of my code that I’m proud of. I had to use different for and while loops in order to create the table to use the months and print the totals each year. This also gave the user the principal and interest each time. I am proud of it because I thought it was pretty cool to just put in these loops and get the effect. I’m proud of how I figured this out so it ran cleanly and gave the user everything they wanted.

 

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def get_month():
    """If user chooses monthly payment, this figures out which month they start in by asking"""
    print()
    monthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    month = input("What month will you pay your first payment on the mortgage? (Please enter the number that corresponds with the month) ")
    while month not in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]:
        month = input("Please enter the number that corresponds with the month: ")
    month = float(month)
    month = month - 1
    monthIndex = int(month)
    return monthList, monthIndex

This part of my code is a little cringy. This part shows how I found out what month the user started the loan. I feel like I could have made this a little different and better. I made a list of the months and then did things like subtracting the number given to find the index which would give me a month. I think there was an easier and more efficient way to do this instead of how I did it. If I had more time I would try to find a better way to do this.

 

Leave a comment

Your email address will not be published. Required fields are marked *