Calculating mortgage interest in Python

BeerHuntor picture BeerHuntor · Apr 22, 2015 · Viewed 39.1k times · Source

I am currently learning python through a video tutorial on youtube, and have come up against a formula I cannot seem to grasp, as nothing looks right to me. The basic concept of the excersise is to make a mortgage calculator that asks the user to input 3 pieces of information, Loan Amount, Interest Rate, and Loan Term (years)

then it calculates the monthly payments to the user. here is my code:

__author__ = 'Rick'
# This program calculates monthly repayments on an interest rate loan/mortgage.

loanAmount = input("How much do you want to borrow? \n")
interestRate = input("What is the interest rate on your loan? \n")
repaymentLength = input("How many years to repay your loan? \n")

#converting the string input variables to float
loanAmount = float(loanAmount)
interestRate = float(interestRate)
repaymentLength = float(repaymentLength)

#working out the interest rate to a decimal number
interestCalculation = interestRate / 100

print(interestRate)
print(interestCalculation)

#working out the number of payments over the course of the loan period.
numberOfPayments = repaymentLength*12

#Formula
#M = L[i(1+i)n] / [(1+i)n-1]

#   * M = Monthly Payment (what were trying to find out)
#   * L = Loan Amount (loanAmount)
#   * I = Interest Rate (for an interest rate of 5%, i = 0.05 (interestCalculation)
#   * N = Number of Payments (repaymentLength)

monthlyRepaymentCost = loanAmount * interestCalculation * (1+interestCalculation) * numberOfPayments / ((1+interestCalculation) * numberOfPayments - 1)
#THIS IS FROM ANOTHER BIT OF CODE THAT IS SUPPOSE TO BE RIGHT BUT ISNT---
# repaymentCost = loanAmount * interestRate * (1+ interestRate) * numberOfPayments  / ((1 + interestRate) * numberOfPayments -1)

#working out the total cost of the repayment over the full term of the loan
totalCharge = (monthlyRepaymentCost * numberOfPayments) - loanAmount


print("You want to borrow £" + str(loanAmount) + " over " + str(repaymentLength) + " years, with an interest rate of " + str(interestRate) + "%!")

print("Your monthly repayment will be £" + str(monthlyRepaymentCost))

print("Your monthly repayment will be £%.2f " % monthlyRepaymentCost)

print("The total charge on this loan will be £%.2f !" % totalCharge)

Everything works, but the value it throws out at the end is completely wrong... a £100 loan with an interest rate of 10% over 1 year shouldn't be making me pay £0.83 per month. Any help in getting my head around this equation to help me understand would be greatly appreciated.

Answer

C. Ramsey picture C. Ramsey · Sep 20, 2017

With the help of examples, this is what I did.

# Formula for mortgage calculator
# M = L(I(1 + I)**N) / ((1 + I)**N - 1)
# M = Monthly Payment, L = Loan, I = Interest, N = Number of payments, ** = exponent

# Declares and asks for user to input loan amount. Then converts to float
loanAmount = input('Enter loan amount \n')
loanAmount = float(loanAmount)

# Declares and asks user to input number of payments in years. Then converts to float. Years * 12 to get
#  total number of months
years = input('How many years will you have the loan? \n')
years = float(years) * 12

# Declares and asks user to input interest rate. Then converts to float and input interest rate is /100/12
interestRate = input('Enter Interest Rate \n')
interestRate = float(interestRate) / 100 / 12

# Formula to calculate monthly payments
mortgagePayment = loanAmount * (interestRate * (1 + interestRate)
                                ** years) / ((1 + interestRate) ** years - 1)

# Prints monthly payment on next line and reformat the string to a float using 2 decimal places
print("The monthly mortgage payment is\n (%.2f) " % mortgagePayment)