CS101 final project: Mortgage Calculator


I am working on my final project for CS101, a mortgage calculator. I am trying to calculate the amortisation schedule to easily identify when the interest is paid off, and print how many payments it will take for this to happen.
in my for-loop below, I am struggling to understand why the value of ‘principle’ is not decreasing, ie when I print out the interest payments list the numbers are the same when they should be decreasing.
See the code below:

#Final project from from the learn Python 3 course on Codecademy
#This is a terminal game
#that will calculate your mortgage based off user input data

print("Welcome to the mortgage calculator terminal game!")
print("Based off the information you provide us, ")
print("We will calculate your mortgage for you.")

loan_length = input("In years, how long is the length of your loan?")
loan_duration = float(loan_length)
loan_amount = input("What is the total value of your loan?")
loan_principle = float(loan_amount)
interest_rate = input("What is the annual interest rate for your loan?")
monthly_interest = float(interest_rate) / 100.0 / 12.0
no_of_payments = int(loan_duration * 12)
#interest_coumpound_rate = input("Is your interest rate \"monthly\" or \"annually?\"")
#repayment_frequency = int(input("""Input the number representing your repayment terms 
                          #1. Monthly
                          #2. Annually

#function to calculate total monthly repayment given above input
def calc_monthly_repayments(loan_duration, loan_principle, monthly_interest):
     monthly_payment = loan_principle * monthly_interest * ((1.0 + monthly_interest)**no_of_payments) / (((1.0 + monthly_interest) ** no_of_payments) - 1.0)
     round_monthly = round(monthly_payment, 2)     
     return round_monthly
total_per_month = calc_monthly_repayments(loan_duration, loan_principle, monthly_interest)  

print(f"Your total monthly payment is AU${total_per_month}.")

def calc_monthly_interest(loan_principle):
    #created a variable holding total principle to have in the scope of the function
    principle = loan_principle
    monthly_principle = loan_principle / (loan_duration * 12)
    total_payment = total_per_month * (loan_duration * 12)
    total_interest = total_payment - loan_principle
    monthly_interest_payment = monthly_interest * principle
    principle_payments = []
    interest_payments = []
    for payment in range(0, (no_of_payments + 1)): #add + 1 to check the final payment is 0
        #reducing total principle amount after each payment     
        principle -= monthly_principle
    #checking to see final payment is 0
    print(f"You are expected to spend a total of ${total_payment} which includes ${total_interest}")

Any feedback is greatly appreciated!

You did not change the value of monthly_principle inside the loop, so you appended the same number to the principle_payments list over and over.
You did not change the value of monthly_interest_payment inside the loop, so you appended the same number to the interest_payments list repeatedly.
(From the code posted, the only variables that change values form one iteration to the next are payment and principle.)

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.