【发布时间】:2015-06-30 12:58:27
【问题描述】:
我目前正在通过 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 英镑的贷款,利率为 10% 超过 1 年不应该让我每月支付 0.83 英镑。任何帮助我理解这个等式以帮助我理解的帮助将不胜感激。
【问题讨论】:
-
某处您只是支付利息而不是增加贷款金额... 0.83 x 12 = ~10
-
利息多久累积一次?每个月?
-
我不确定,这是我在模块之后得到的指令。创建一个抵押/贷款计算器。 * 让用户输入贷款成本、利率和贷款年数 * 使用以下公式计算每月还款额 * * M = L[i(1+i)n] / [(1 +i)n-1] * M = 每月还款 * L = 贷款金额 * I = 利率(对于 5% 的利率,i = 0.05 * N = 还款次数
-
这个程序不计算复利或单利。它也没有提供复利期,它也错误地显示了您的总费用,不要减去贷款金额。它也只是将您的利率除以您的付款次数以获得每月付款金额,并且实际上并未使用复利或单利的公式。你真的没有正确实现注释掉的公式,你可能想再看看。
-
这取决于抵押贷款供应商的小字,他们准确地说出“年利率”的含义:-) 处理它的一种粗略(但不是真正正确)的方法是将年利率除以利率提高 12 倍。使用
monthly_rate = (1 + annual_rate)**(1/12.) - 1之类的东西会更好。
标签: python python-3.x formula calculator