I have a project i'm working on for school and I'm pretty sure I've got it correct, but i'm not sure how to check the output. I just need an idea of whether or not i'm going about this correctly. The assignment is to have it display the monthly payment and the total interest. Here's my code
principal = float(input("Input Loan Principal: "))
# Principal amount borrowed input by user
loan_term = float(input("Input Loan Term In Years: "))
# Loan Term in years input by user
loan_interest = float(input("Input Loan Interest Rate: "))
# Loan interest in percentage input by user
monthly_rate = loan_interest / 100
# Loan interest percentage to decimal
loan_term_months = loan_term * 12
# Loan term from years to months
balance = principal
# Separate variable for calculations
math1 = (1.0 + monthly_rate) ** loan_term
math2 = (monthly_rate/(math1 - 1.0))
# Calculations
monthly_payment = (monthly_rate + math2) * balance
# Final Calculation for monthly payment
interest = (monthly_payment * loan_term) - principal
# Final Calculation for interest paid
final_monthly_payment = str(round(monthly_payment, 2))
final_interest = str(round(interest, 2))
# Rounding to two decimal points
print("Monthly payment: ", final_monthly_payment)
print("Effective Interest Paid: ", final_interest)
# Final print
Thanks for any help in advance
Here's a fancy way to check your answer using scipy fsolve:
import numpy as np
from scipy.optimize import fsolve
fv = 0
pv = 200000
rate = 0.075 / 12
nper = 15 * 12
def f(pmt):
return fv + pv*(1 + rate)**nper + pmt*(1 + rate*0) / rate*((1 + rate)**nper - 1)
pmt_amt = fsolve(f, [100], xtol=0.000001)
print(pmt_amt)
Printing result
returns:
[-1854.02472001]
The main concept above is to let scipy solve for the variable pmt
in the formula... which is the payment amount. Interpretation is that a payment of $1,854.02 would be needed to pay off a $200,000 loan in 15 years at 7.5%.
To see how much principal and interest goes towards each payment, numpy again could help you out with np.ppmt
and np.ipmt
. Example:
import numpy as np
fv = 0
pv = 200000
rate = 0.075 / 12
nper = 15 * 12
for per in range(nper):
principal = -np.ppmt(rate, per, nper, pv)
interest = -np.ipmt(rate, per, nper, pv)
print(principal, interest, principal + interest)
Returns:
600.27301367 1253.75170634 1854.02472001
604.024720005 1250.0 1854.02472001
607.799874505 1246.2248455 1854.02472001
611.598623721 1242.42609628 1854.02472001
615.421115119 1238.60360489 1854.02472001
619.267497089 1234.75722292 1854.02472001
623.137918946 1230.88680106 1854.02472001
627.032530939 1226.99218907 1854.02472001
630.951484257 1223.07323575 1854.02472001
...
1797.15714344 56.8675765608 1854.02472001
1808.38937559 45.6353444143 1854.02472001
1819.69180919 34.3329108169 1854.02472001
1831.064883 22.9598370094 1854.02472001