I'm having an issue with my simple code that is suppose to be a mortgage calculator where all the rates from 0.03 to 0.18 are listed in a table. Here is my code and error.
l = 350000 #Loan amount
n = 30 #number of years for the loan
r = [0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18] #interest rate in decimal
n = n * 12
a = l
int1 = 12
u = [x / int1 for x in r]
D = (((u+1)**n)-1) /(u*(u+1)**n)
z = (a / D)
print(z)
File "test.py", line 23, in <module>
D = (((u+1)**n)-1) /(u*(u+1)**n)
TypeError: can only concatenate list (not "int") to list
Thanks
The problem is that u is a list which cannot be used for vectorized operation which you are doing while computing D. You can convert your list to a NumPy array to make your code work.
u = np.array([x / int1 for x in r])
Alternatively, you can use a for loop or list comprehension to store D for each element of u as
D = [(((i+1)**n)-1) /(i*(i+1)**n) for i in u]
but this will again complain during z = (a / D) because D is still a list. Therefore, converting to array seems to be a convenient approach.
The another alternative answer is to compute z using list comprehension directly without involving extra variable D
z = [a / ((((i+1)**n)-1) /(i*(i+1)**n)) for i in u]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With