我在我的一个大学课程中学习Python,并且我被要求创建一个“贷款计算器”....我可能有一个想法,但我不知道如何修复我得到的错误TypeError: 'float' object is not subscriptable
这是公告 用户必须输入贷款成本,利率和贷款年数。 使用以下公式计算每月付款:
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
这是我的代码:
# 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:我首先想到的是我错过了将“M”转换为字符串,但在我改为
之后print("Monthly payment: " + str(M))
我仍然遇到同样的错误......请帮助!
答案 0 :(得分:0)
需要做一些改变:
rescue_from ActionController::UnpermittedParameters do |e|
render json: {error: e.message, params: e.params}, status: :unprocessable_entity
end
使用一些任意值:
# 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)