TheWolf
TheWolf

Reputation: 1695

Python looping issue with variable not being assigned to on certain iteration

I am new to Python, I am experiencing some bug where on a certain iteration a variable declared outside of a loop is not being assigned to only after a specific iteration.

Line 28 on the 8th iteration (x == 7) interest is miscalculated (probably not assigned to or something) and remains the same value for the rest of the iterations. The variable value seems to get stuck.

import matplotlib.pyplot as plt 
import numpy as np 

loanAmount = 500000.0
interestRate = 3.5
loanTerm = 30.0

monthlyInterest = []
monthlyPrincipal = []
compoundingRate = float((((interestRate / 12) * 0.01) + 1))
loanTermMonths = float(loanTerm * 12)
preInterestMonthly = loanAmount / loanTermMonths
lastMonthWithInt = preInterestMonthly * compoundingRate

i = 1
sum = 0.0
while i < loanTermMonths - 1:
    lastMonthWithInt = lastMonthWithInt * compoundingRate
    sum += lastMonthWithInt
    i += 1

baseMonthly = sum / loanTermMonths

interest = 0.0
principal = 0.0
x = 0
while x < loanTermMonths - 1:
    interest = float((loanAmount - principal) * (compoundingRate - 1))   
    principal = baseMonthly - interest
    monthlyInterest.append(interest)
    monthlyPrincipal.append(principal)
    x += 1

x1 = np.arange(1, loanTermMonths)
y1 = np.array(monthlyInterest)
x2 = np.arange(1, loanTermMonths)
y2 = np.array(monthlyPrincipal)

plt.plot(x1, y1, label = "Interest") 
plt.plot(x2, y2, label = "Principal") 
plt.xlabel('months') 
plt.ylabel('$') 
plt.show()

Upvotes: 0

Views: 84

Answers (2)

Chris Charley
Chris Charley

Reputation: 6573

I think if you enter the following statement right below

monthlyPrincipal.append(principal)

loanAmount -= principal,

then you will get the results you want.

EDIT: Just a suggestion, to get a more accurate monthly payment, I got from Wikipedia a formula to calculate the monthly payments given an initial loan amount.

Here is a program that uses that formula.

import matplotlib.pyplot as plt 
# https://stackoverflow.com/questions/52845238/python-looping-issue-with-variable-not-being-assigned-to-on-certain-iteration
loanAmount = 500000.0
interestRate = 3.5
loanTerm = 30 # 30 years
loanTermMonths = loanTerm * 12 # 360 months

i = interestRate / 12 * .01
n = loanTermMonths

# get cash flows, (monthly payments), for Present Value of loanAmount
# https://en.wikipedia.org/wiki/Present_value
monthlyPayment = loanAmount * i / (1 - (1 + i)**(-n))

principal = loanAmount

monthlyInterest = []
monthlyPrincipal = []

for x in range(loanTermMonths):
    interest = principal * i
    monthlyInterest.append(interest)

    principalPortion = monthlyPayment - interest
    monthlyPrincipal.append(principalPortion)

    principal -= principalPortion

x1 = list(range(1, loanTermMonths+1))
y1 = monthlyInterest
x2 = list(range(1, loanTermMonths+1))
y2 = monthlyPrincipal

plt.plot(x1, y1, label = "Interest") 
plt.plot(x2, y2, label = "Principal")
plt.title('Loan Amortization')
plt.grid(True)
plt.xlabel('months') 
plt.ylabel('$') 
plt.show()

It is roughly the same as yours except for how it calculates the monthly payment.

Upvotes: 1

tormich
tormich

Reputation: 43

>>> (100.0 - 6.805590751165351) * (1.0029166666666666 - 1)

0.2718170269757688

>>> (100.0 - 6.805590751165351) * (1.0029166666666667 - 1)

0.2718170269757688

i think you need more precision

Upvotes: 0

Related Questions