# Basic Mortgage Calculator

Hi, I’m a respiratory therapist teaching myself how to code with Codecademy. I’ve created this mortgage calculator as part of my CS101 final project. I don’t know much about mortgages and I’m still a beginner with coding so this is a very simple program with just 3 parts. I’d appreciate any feedback from the community!

Step #1: I created a Mortgage class that uses information input by the user to perform 2 different methods (just two, for now). The attributes that every Mortgage loan has are the interest rate, the loan amount, and the length of the loan.

Other mortgage attributes that the user doesn’t input but that every mortgage has are the monthly interest rate and monthly mortgage amount.

The two methods I included in this program are a method to calculate the monthly payments the user will make on the loan and another method to calculate the total amount the user has to make overall (assuming it was a fixed rate loan).

``````class Mortgage:
def __init__(self, user_name, interest_rate, loan_amount, loan_length):
self.user_name = user_name
self.interest_rate = interest_rate
self.loan_amount = loan_amount
self.loan_length = loan_length
self.monthly_interest = self.interest_rate/100/12
self.monthly_mortgage = (self.monthly_interest * self.loan_amount) / (1-((1 + self.monthly_interest))** (-self.loan_length * 12))

def calculate_monthly(self):
formatted_monthly_mortgage = "\${:,.2f}".format(self.monthly_mortgage)
print(f"{self.user_name}, your monthly payments on a loan of this amount would be {formatted_monthly_mortgage}.")

def calculate_total_loan(self):
total_loan_cost = (self.monthly_mortgage * self.loan_length) + self.loan_amount
dollar_format = "\${:,.2f}".format(total_loan_cost)
print(f"{self.user_name}, assuming your interest rate remains at {self.interest_rate}% throughout the length of the loan, the total amount you'll have to pay back is {dollar_format}.")
``````

Step #2: I created a function that asks the user to input the interest rate, loan amount, and loan term and appends all this information into a list for use as the attributes when an instance of the Mortgage class is created.

I also imported the time module, using it in a for loop at the end of the get_info function to create the effect that the computer is “thinking” and processing the information.

``````import time
def get_info():

information = []
interest_rate = input("Okay! I can help with that! When typing the answers to the questions I ask, type ONLY the number or decimal points. Tell me, what is the interest rate on your loan? \n")
information.append(float(interest_rate))
loan_amount = input(f"Got it, your interest rate is {interest_rate}%! And how much would you like to borrow? \n")
information.append(float(loan_amount))
loan_length = input(f"Okay, you'd like to borrow {loan_amount}. And in years, tell me the length of this loan. \n")
information.append(float(loan_length))
print("Please wait while I process this information!")
for i in range(3):
print(".....")
time.sleep(2)
return information
``````

Step #3: The program starts with the computer introducing itself and asking the user’s name along with the other information it needs to perform its tasks. Once it gets the information it requires, an instance of the Mortgage class is instantiated, and the program performs the method the user requested. When the user is done with one task, they’re given the options to perform another task.

``````get_name = input("Hi! My name is Morty, and I am a mortgage calculater. What is your name? ")
print("Hi, {get_name}! So nice to meet you. What would you like to do today?")
continuing = True
while continuing:
get_task = input(f"Type '1' to calculate your monthly interest rate.\nType '2' to calculate the total amount of your loan.\n")
information = get_info()
interest_rate, loan_amount, loan_length = information, information, information
new_user = Mortgage(get_name, interest_rate, loan_amount, loan_length)