贷款计算器的Python编码问题

贷款计算器的Python编码问题,python,calculator,Python,Calculator,我试图在一个网站上创建一个贷款计算器,但在Python的编码方面遇到了麻烦。代码是: # user enter the cost of the loan, the interest rate, and #the number of years for the loan #Calculate monthly payments with the following formula # M = L[i(1+i)n] / [(1+i)n-2] # M = monthly payment # L = Lo

我试图在一个网站上创建一个贷款计算器,但在Python的编码方面遇到了麻烦。代码是:

# user enter the cost of the loan, the interest rate, and
#the number of years for the loan
#Calculate monthly payments with the following formula
# M = L[i(1+i)n] / [(1+i)n-2]
# M = monthly payment
# L = Loan amount
# i = interest rate (for an interest rate of 5%, i = 0.05)
# n = number of payments
#________________________________________________________________________#
#Start of program
#Declare variables

monthlypayment = 0  
loanamount = 0
interestrate = 0
numberofpayments = 0  
loandurationinyears = 0
loanamount = raw_input("Lending Money ")
interestrate = raw_input("Interest Rates are? ")
loandurationinyears = raw_input("Time Duration in Years?")
#Convert the strings into floating numbers so we can use them in the formula
loandurationinyears = float(loandurationinyears)
loanamount = float(loanamount)
interestrate = float(interestrate)
#Since payments are once per month, number of payments is number of years for the loan
payments = loaninyears*12
#calculate the monthly payment based on the formula
payment = amount * interestrate * (7+ interestrate) * payments / ((1 + interestrate) * payments -1)
#Result to the program
print("Payment will be " + st(monthlypayment))

有经验的人能帮我找到这个编码中的语法或其他逻辑错误吗?

您正在读取以前未声明的变量。 将loaninyears更改为LoanDurationin Years,并将金额更改为loanamount

此外,您在最后一行有一个输入错误,st应该是str

还有一些提示:

首先,你可以做以下事情:

input = float(raw_input("Give me some number"))
这样可以缩短程序的长度

也可以考虑使用更可读的变量命名,例如:


loanInYears或loan_in_years

严格遵循评论中的公式,使用Python 2.7运行,但结果不正确

# user enter the cost of the loan, the interest rate, and
#the number of years for the loan
#Calculate monthly payments with the following formula
# M = monthly payment
# L = Loan amount
# i = interest rate (for an interest rate of 5%, i = 0.05)
# n = number of payments

L = input ('loan amount')
i = input ('interest rate')
n = input ('nr of payments')

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

print (M)
我认为你应该首先修正你的公式,除了编码错误。 我特别想念12号,因为你的利息是每年的,但你的付款是每月的

[编辑]

看看乔希的答案:

也许可以使用不同的视频教程,因为这一教程似乎会让很多人陷入困境


提示:如果您有非常长的可变名称,您可以在其中添加一些下划线,但贷款年数未定义。您将它乘以12。
print(“付款将是”+st(monthlypayment))
我认为
st
应该是
str
这里的金额可能应该是loanamount,并且您从来没有给monthlypayment一个0以外的值