python - 在 Python 中计算抵押贷款利息

标签 python python-3.x formula calculator

我目前正在通过 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)

一切正常,但它最终抛出的值(value)是完全错误的...1 年利率为 10% 的 100 英镑贷款不应该让我每月支付 0.83 英镑。任何帮助我理解这个等式以帮助我理解的帮助将不胜感激。

最佳答案

在示例的帮助下,这就是我所做的。

# Formula for mortgage calculator
# M = L(I(1 + I)**N) / ((1 + I)**N - 1)
# M = Monthly Payment, L = Loan, I = Interest, N = Number of payments, ** = exponent

# Declares and asks for user to input loan amount. Then converts to float
loanAmount = input('Enter loan amount \n')
loanAmount = float(loanAmount)

# Declares and asks user to input number of payments in years. Then converts to float. Years * 12 to get
#  total number of months
years = input('How many years will you have the loan? \n')
years = float(years) * 12

# Declares and asks user to input interest rate. Then converts to float and input interest rate is /100/12
interestRate = input('Enter Interest Rate \n')
interestRate = float(interestRate) / 100 / 12

# Formula to calculate monthly payments
mortgagePayment = loanAmount * (interestRate * (1 + interestRate)
                                ** years) / ((1 + interestRate) ** years - 1)

# Prints monthly payment on next line and reformat the string to a float using 2 decimal places
print("The monthly mortgage payment is\n (%.2f) " % mortgagePayment)

关于python - 在 Python 中计算抵押贷款利息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29804843/

相关文章:

python - gtk.gdk.color_parse() 等效于 vala

python - Django URL 与任何 URL 配置都不匹配

python - PyUnit - 如何为单个单元测试添加超时

python - 将返回的元组分配给数组的不同列

python - 如何计算数组中每个值的公式?

python - 有人可以帮我理解这个简短的 .py

python - pyqt5 应用程序中的 matplotlib 工具栏

python-3.x - 谷歌合作实验室 : OSError: [Errno 5] Input/output error

excel - 将数据模型数据用于单元格公式

algorithm - Trifecta 和 Superfecta 等奇特赌注的计算公式