Python Forum
Need some help trying to get my Python mortgage amortization calculator to work prope
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help trying to get my Python mortgage amortization calculator to work prope
#1
So, I will be starting a graduate program in Data Science in Spring 2018 and I am trying to get acquainted with Python before then, but I am having some difficulty with a problem I am working on to force myself to better understand how this works.

Here is the exact description of the problem:

In this problem, you will create a mortgage calculator that takes as input the principal loan amount, interest rate, and monthly payment. As output, your calculator should generate an amortization table, and compute how many years and months it took to pay off the mortgage, and report the total amount of payments over that time.

In a mortgage, the bank lends you a certain amount of principal to purchase a house at a certain interest rate. Every month, the amount you owe (the balance) first increases due to interest: 1/12 of the interest rate times the current balance. Then the balance decreases due to your monthly payment. For example, suppose you borrow $100,000 at 5% annual interest, with $500 monthly payments. In the first month, the interest increases the balance by $416.67, and then your payment reduces it by $500, for a remaining balance of $99,916.67. In the second month, the interest charge is $416.32, and the remaining balance is $99,832.99.

If you continue this process, you get an amortization table like this:

Expected Mortgage Amortization Table Outcome

[Image: iVrouyo.png]

You can also compute the total amount of time and money to pay off the mortgage. In this example, it takes 35 years and 11 months, and the total amount paid is approximately $215,458.84.

Here are some things to consider when implementing the calculator:

For dollar values, only display two digits of precision after the decimal point. (You can assume that the banke will not round internally).
The final payment will almost certainly be smaller than the others, so be careful to check for that case so you don’t end up with a negative balance.
If the monthly payment is too small, the balance will go up every month! If this happens, the program should stop and display an appropriate error message.

If you accidentally create an infinite loop, try to interrupt or restart the kernel.
Use the modulus % operator to separate the years and months.
Your program should only display the columns requested, and the rows should be formatted to appear under the appropriate heading.

Here is the code I have so far:

loanamount = 100000
interestrate = 5
monthlypayment = 500

monthlyinterest = round((loanamount * interestrate/100/12),2)
monthlybalance = float(loanamount - (monthlypayment - monthlyinterest)) 

print ("Month", "\t\t", "Payment", "\t\t", "Interest", "\t\t\t", "Balance")
print ("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")

month = 0

while monthlybalance > 0 :
        month += 1

##### This is where I am completely stuck and I think I need a “for” loop in here, but this is where I am LOST #####

        print (month, "\t\t", monthlypayment, "\t\t\t", interest, "\t\t\t", balance)
Here is the Mortgage Amortization Table that is being created using my existing code above, but obviously this looks nothing like the Mortgage Amortization Table above for rows 2 and on:

My Mortgage Amortization Table

[Image: 5fuMxg0.png]

I need each value in the Interest and Balance columns to decrease rather than have the same values and this is what I need help with.

Any support here from you gurus would be MUCH appreciated.

Thank you all.
Reply
#2
You don't need a for loop in the while loop. The while loop is all you need. You just need to recalculate the values in the while loop, so they change each time through the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
also, you use monthlybalance to control loop,
but print out balance
make sure names are consistant
Reply
#4
I remember Menator mentioned a module called tabulate to present data neatly, you might look at that. I have never used it.

Though not an expert, I thought I would look at what you have written:

loanamount = 100000
interestrate = 5
monthlypayment = 500
 
monthlyinterest = round((loanamount * interestrate/100/12),2)
monthlybalance = round(float(loanamount - (monthlypayment - monthlyinterest)),2) 
 
print ("Month", "\t\t", "Monthly Payment", "\t\t", "Interest", "\t\t\t", "Monthly Balance")
print ("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")
 
month = 0
 
while monthlybalance > 0 :
    month += 1
    monthlyinterest = round((loanamount * interestrate/100/12),2)
    monthlybalance = round(float(loanamount - (monthlypayment - monthlyinterest)),2) 
    loanamount = monthlybalance
    print (month, "\t\t", monthlypayment, "\t\t", interestrate, "\t\t", monthlyinterest, "\t\t", monthlybalance)
    
print('Congratulations! Despite constant Bank of England interest rate hikes, you managed to pay off your mortgage!')
print('Many others were not so lucky!')
print('Loanamount now:', loanamount)
Reply
#5
(Sep-13-2017, 11:26 PM)IamSirAskAlot Wrote: So, I will be starting a graduate program in Data Science in Spring 2018 and I am trying to get acquainted with Python before then, but I am having some difficulty with a problem I am working on to force myself to better understand how this works.

Here is the exact description of the problem:

In this problem, you will create a mortgage calculator that takes as input the principal loan amount for a loan like lendy that you can find here https://fitmymoney.com/loans-like-lendly/,interest rate, and monthly payment. As output, your calculator should generate an amortization table, and compute how many years and months it took to pay off the mortgage, and report the total amount of payments over that time.

In a mortgage, the bank lends you a certain amount of principal to purchase a house at a certain interest rate. Every month, the amount you owe (the balance) first increases due to interest: 1/12 of the interest rate times the current balance. Then the balance decreases due to your monthly payment. For example, suppose you borrow $100,000 at 5% annual interest, with $500 monthly payments. In the first month, the interest increases the balance by $416.67, and then your payment reduces it by $500, for a remaining balance of $99,916.67. In the second month, the interest charge is $416.32, and the remaining balance is $99,832.99.

If you continue this process, you get an amortization table like this:

Expected Mortgage Amortization Table Outcome

[Image: iVrouyo.png]

You can also compute the total amount of time and money to pay off the mortgage. In this example, it takes 35 years and 11 months, and the total amount paid is approximately $215,458.84.

Here are some things to consider when implementing the calculator:

For dollar values, only display two digits of precision after the decimal point. (You can assume that the banke will not round internally).
The final payment will almost certainly be smaller than the others, so be careful to check for that case so you don’t end up with a negative balance.
If the monthly payment is too small, the balance will go up every month! If this happens, the program should stop and display an appropriate error message.

If you accidentally create an infinite loop, try to interrupt or restart the kernel.
Use the modulus % operator to separate the years and months.
Your program should only display the columns requested, and the rows should be formatted to appear under the appropriate heading.

Here is the code I have so far:

loanamount = 100000
interestrate = 5
monthlypayment = 500

monthlyinterest = round((loanamount * interestrate/100/12),2)
monthlybalance = float(loanamount - (monthlypayment - monthlyinterest)) 

print ("Month", "\t\t", "Payment", "\t\t", "Interest", "\t\t\t", "Balance")
print ("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")

month = 0

while monthlybalance > 0 :
        month += 1

##### This is where I am completely stuck and I think I need a “for” loop in here, but this is where I am LOST #####

        print (month, "\t\t", monthlypayment, "\t\t\t", interest, "\t\t\t", balance)
Here is the Mortgage Amortization Table that is being created using my existing code above, but obviously this looks nothing like the Mortgage Amortization Table above for rows 2 and on:

My Mortgage Amortization Table

[Image: 5fuMxg0.png]

I need each value in the Interest and Balance columns to decrease rather than have the same values and this is what I need help with.

Any support here from you gurus would be MUCH appreciated.

Thank you all.

You're on the right track, and you just need to adjust the calculation inside the loop to update the interest and balance correctly.

loanamount = 100000
interestrate = 5
monthlypayment = 500

monthlyinterest = round((loanamount * interestrate/100/12), 2)
monthlybalance = loanamount

print ("Month", "\t\t", "Payment", "\t\t", "Interest", "\t\t\t", "Balance")
print ("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")

month = 0

while monthlybalance > 0:
    month += 1
    interest = round((monthlybalance * interestrate/100/12), 2)
    
    # Check if the monthly payment is too small
    if monthlypayment <= interest:
        print("Error: Monthly payment is too small. Balance will increase every month.")
        break
    
    # Check if the final payment is smaller than the calculated interest
    if monthlybalance < monthlypayment:
        monthlypayment = monthlybalance + interest

    balance = round(monthlybalance - (monthlypayment - interest), 2)
    
    print (month, "\t\t", monthlypayment, "\t\t\t", interest, "\t\t\t", balance)

    # Update the balance for the next iteration
    monthlybalance = balance

# Calculate years and months
years = month // 12
months = month % 12

# Print total time and amount paid
print("\nTotal time to pay off mortgage: {} years and {} months".format(years, months))
print("Total amount paid: ${:.2f}".format(monthlypayment * month))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python, trying to make a basic calculator AthertonH 2 1,144 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  How To Create A "Birthday Calculator" in Python? unigueco9 3 3,712 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  python calculator only using decomposing functions kirt6405 1 1,773 Jun-19-2021, 12:52 AM
Last Post: bowlofred
  Python calculator divide by zero help dock1926 4 5,860 Jan-20-2020, 05:15 PM
Last Post: michael1789
  BEGINNER: My calculator doesnt work iskov 5 3,208 Oct-09-2019, 07:45 AM
Last Post: buran
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,191 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Creating a Calculator with Python KatherineHov 8 7,749 Aug-03-2017, 02:13 PM
Last Post: sparkz_alot
  Python Calculator Application MemeStealer 6 7,807 Jul-21-2017, 08:42 PM
Last Post: MemeStealer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020