简体   繁体   English

贷款计算器的 Python 编码问题

[英]Python Coding Issue for Loan Calculator

I am trying to create a loan calculator on a website and I am getting trouble in the coding of Python.我正在尝试在网站上创建贷款计算器,但在 Python 编码方面遇到了麻烦。 The code is:代码是:

# 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))

Can any experienced person help me to get the syntax or other logical errors in this coding?任何有经验的人都可以帮助我了解此编码中的语法或其他逻辑错误吗?

You are reading variables that you have not previously declared.您正在读取之前未声明的变量。 Change loaninyears to loandurationinyears and amount to loanamount .更改loaninyearsloandurationinyears数量loanamount。

Additionally you have a typo in the last line, st should be str另外你在最后一行有一个错字, st应该是str

Also some tips:还有一些提示:

First, you can do stuff like:首先,您可以执行以下操作:

input = float(raw_input("Give me some number"))

This way you can shorten the length of your program.这样您就可以缩短程序的长度。

Also you might want to consider using a more readable variable naming, for example:此外,您可能需要考虑使用更具可读性的变量命名,例如:

loanInYears or loan_in_years贷款年数贷款年数

Following your formula in the comments closely, and using Python 2.7 , this runs, but the outcome isn't correct.密切关注您在评论中的公式,并使用 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)

I think you should fix your formula first, apart from coding errors.我认为除了编码错误之外,您应该首先修复您的公式。 Specifically I miss the number 12 somewhere, since your interest is per year, but your payments are per month.具体来说,我想念某个地方的数字 12,因为您的利息是每年,但您的付款是每月。

[EDIT] [编辑]

Look at the answer of Joshy here:看看乔希在这里的回答:

Formula for calculating interest Python Python利息计算公式

and perhaps use a different video tutorial, since this one seems to get many folks in trouble.也许使用不同的视频教程,因为这个教程似乎让很多人陷入困境。

Tip: ifyouhaveverylongvariablenames you_may_place_some_underscores_in_them提示:如果你有很长的变量名 you_may_place_some_underscores_in_them

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM