Waldo - 8 months ago 52

Python Question

I´m learning Python at one of my college classes and I was asked to create a "Loan Calculator".... I might have an idea but I´m not sure how to fix an error that I´m getting

`TypeError: 'float' object is not subscriptable`

This is the announcement

The user has to enter the cost of the loan, interest rate and the number of years of the loan.

Calculate the monthly payments with the following formula:

`M = L[i(1+i)^n]/[(1+i)^(n)-1]`

Data:

M = monthly payment

L = loan amount

i = interest rate (remember that 5%, i = 0.05)

n = number of payments

And this is my code:

`# Loan Calculator`

# Equation: M = L[i(1+i)^n]/[(1+i)(n)-1]

print("Loan Calculator")

L = float(input("Loan amount: "))

i = float(input("Interest rate: "))

# Remember: 5% ---> i=0.05

n = float(input("Number of payments: "))

M = (L[i*(1+i)**n]/[(1+i)**(n)-1])

# M = Monthly payment

print("Monthly payment: " + M)

PS: I first thought I was missing convert "M" into a string, but after I changed to

`print("Monthly payment: " + str(M))`

I'm still getting the same error... Please help!

Answer

Needed a few changes:

```
# Loan Calculator
# Equation: M = L[i(1+i)^n]/[(1+i)(n)-1]
print("Loan Calculator")
L = float(input("Loan amount: "))
i = float(input("Interest rate: "))
# Remember: 5% ---> i=0.05
n = float(input("Number of payments: "))
M = L*(i*(1+i)**n)/((1+i)**(n)-1)
# M = Monthly payment
print("Monthly payment: " , M)
```

Using some arbitrary values:

```
Loan Calculator
Loan amount: 1000
Interest rate: 9
Number of payments: 100
('Monthly payment: ', 9000.0)
```

Source (Stackoverflow)