我目前正在通过youtube上的视频教程学习python,并且遇到了一个我似乎无法掌握的公式,因为在我看来没有什么是正确的。其基本概念是制作一个抵押计算器,要求用户输入3条信息、贷款金额、利率和贷款期限(年)
然后它计算每月支付给用户的款项。这是我的代码:
__author__ = 'Rick'
# This program calculates monthly repayments on an interest rate loan/mortgage.
loanAmount = input("How much do you want to borrow? \n")
interestRate = input("What is the interest rate on your loan? \n")
repaymentLength = input("How many years to repay your loan? \n")
#converting the string input variables to float
loanAmount = float(loanAmount)
interestRate = float(interestRate)
repaymentLength = float(repaymentLength)
#working out the interest rate to a decimal number
interestCalculation = interestRate / 100
print(interestRate)
print(interestCalculation)
#working out the number of payments over the course of the loan period.
numberOfPayments = repaymentLength*12
#Formula
#M = L[i(1+i)n] / [(1+i)n-1]
# * M = Monthly Payment (what were trying to find out)
# * L = Loan Amount (loanAmount)
# * I = Interest Rate (for an interest rate of 5%, i = 0.05 (interestCalculation)
# * N = Number of Payments (repaymentLength)
monthlyRepaymentCost = loanAmount * interestCalculation * (1+interestCalculation) * numberOfPayments / ((1+interestCalculation) * numberOfPayments - 1)
#THIS IS FROM ANOTHER BIT OF CODE THAT IS SUPPOSE TO BE RIGHT BUT ISNT---
# repaymentCost = loanAmount * interestRate * (1+ interestRate) * numberOfPayments / ((1 + interestRate) * numberOfPayments -1)
#working out the total cost of the repayment over the full term of the loan
totalCharge = (monthlyRepaymentCost * numberOfPayments) - loanAmount
print("You want to borrow £" + str(loanAmount) + " over " + str(repaymentLength) + " years, with an interest rate of " + str(interestRate) + "%!")
print("Your monthly repayment will be £" + str(monthlyRepaymentCost))
print("Your monthly repayment will be £%.2f " % monthlyRepaymentCost)
print("The total charge on this loan will be £%.2f !" % totalCharge)
一切正常,但它在最后抛出的价值是完全错误的。。。100英镑的贷款,利率超过1年10%,不应该让我每月支付0.83英镑。如果能帮助我理解这个方程式,我将不胜感激。
我也看了这段youtube视频,也遇到了同样的问题。 我是个Python新手,所以请容忍我。 视频里的老师用的是Python3,我用的是Python2 所以我不确定所有的语法是否都一样。但是使用这个线程中的一些信息和这个链接中的信息:(http://www.wikihow.com/Calculate-Mortgage-Payments)我能够得到答案。(我的计算结果与在线抵押贷款计算器相符)
显然你把公式抄错了。
注:在公式中出现两次 注意:在python中,
**
是“to the power of”操作符。在例子的帮助下,这就是我所做的。
相关问题 更多 >
编程相关推荐