So I'm a beginner and I am trying to make a simple mortgage calculator. This is my code:
L=input('Enter desired Loan amount: ')
I=input('Enter Interest Rate: ')
N=input('Enter time length of loan in months: ')
MonthlyPayments= [float(L)*float(I)*(1+float(I))*float(N)]/((1+float(I))*float(N)-1)
print('Your Monthly Payments will be {0:.2f}'.format(MonthlyPayments))`
and i get an error that says: unsupported operand type(s) for /: 'list' and 'float'
答案 0 :(得分:1)
Firstly using square brackets will create a list which is probably not what you want. Also to avoid having to convert constantly you can (and should) wrap your input calls with the type you're expecting to get.
So to go from your example code, I would write it thusly:
L=float(input('Enter desired Loan amount: '))
I=float(input('Enter Interest Rate: '))
N=float(input('Enter time length of loan in months: '))
MonthlyPayments = (L*I*(1+I)*N)/((1+I)*N-1)
print('Your Monthly Payments will be {0:.2f}'.format(MonthlyPayments))
This also makes it easier to read
答案 1 :(得分:0)
MonthlyPayments= (float(L)*float(I)*(1+float(I))*float(N))/((1+float(I))*float(N)-1)
'[' and ']' create a list.
答案 2 :(得分:0)
Here:
MonthlyPayments = [float(L)*float(I)*(1+float(I))*float(N)]/((1+float(I))*float(N)-1)
This part:
[float(L)*float(I)*(1+float(I))*float(N)]
Gives a 'list' data type. Replace []
by ()