在Python中计算抵押贷款利息

10

我目前正在通过YouTube上的视频教程学习Python,并遇到一个公式似乎无法理解,因为对我来说看起来都不对。练习的基本概念是创建一个抵押贷款计算器,要求用户输入三个信息:贷款金额、利率和贷款期限(年)。

然后它会为用户计算每月付款额。下面是我的代码:

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

所有东西都工作正常,但最后抛出的价值完全错误...一笔年利率为10%,金额为£100的贷款在1年内不应该让我每月支付£0.83。如果有帮助理解这个方程式的任何帮助,将不胜感激。


有时候你只是在支付利息而没有增加贷款金额... 0.83 x 12 = ~10 - MrAlexBailey
我不确定,这是我在模块中得到的指示。 创建一个抵押/贷款计算器。
  • 要求用户输入贷款成本、利率和贷款年限
  • 使用以下公式计算每月付款金额
    • M = L[i(1+i)n] / [(1+i)n-1]
  • M = 每月付款金额
  • L = 贷款金额
  • I = 利率(例如5%的利率,i = 0.05)
  • N = 付款次数
- BeerHuntor
该程序不计算复利或简单利息。它也没有提供复利期,还错误地显示了您的总费用,不要减去贷款金额。它只是将您的利率除以您的付款次数来获得每月付款金额,并且实际上并没有使用任何复利或简单利息公式。你真的没有正确地实现注释掉的公式,你可能需要再看一下。 - miradulo
那似乎更好,最终价值现在是; 你想借100英镑,期限为1年,利率为10%! 你的每月还款额将为14.676331510028723英镑 你的每月还款额将为14.68英镑 这笔贷款的总费用将为76.12英镑!@MarkDickinson 我该如何调整以反映年利率而不是每个期间的利率?抱歉,我真的很糟糕,所以这完全超出了我的能力范围。 - BeerHuntor
1
这要看抵押贷款供应商的小字条款,他们准确地解释了"年利率"的意思 :-) 一个粗略(但并不真正正确)的处理方法是将年利率除以12。稍微好一点的方法是使用类似于monthly_rate = (1 + annual_rate)**(1/12.) - 1的东西。 - Mark Dickinson
显示剩余5条评论
6个回答

6

通过示例,这就是我所做的。

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

5
这个抵押贷款包很好用:
>>> import mortgage
>>> m=mortgage.Mortgage(interest=0.0375, amount=350000, months=360)
>>> mortgage.print_summary(m)
                     Rate:      0.037500
             Month Growth:      1.003125
                      APY:      0.038151
             Payoff Years:            30
            Payoff Months:           360
                   Amount:     350000.00
          Monthly Payment:       1620.91
           Annual Payment:      19450.92
             Total Payout:     583527.60

0

这对我来说效果还不错。虽然不是很精确,因为贷款计算器通常按天计算,而我比较懒,所以按月计算,但这是我写的一个简单的计算器,足够准确实用。

L = input("How much will you be borrowing? ")
L = float(L)
print(L)


N = input("How many years will you be paying this loan off? ")
N = float(N) *12
print(N)

I = input("What is the interest in percents that you will be paying? Ex, 10% = 10, 5% = 5, etc. ")
I = float(I)/100
print(I)

M = (L/N) + I*(L/N)

float(M) 
print("Your monthly payment will be: ")
print(M)

0

看起来你复制公式的时候出错了。

wrong:   * numberOfPayments 

correct: ** numberOfPayments 

注意:公式中出现了两次 注意:在Python中,**是“乘方”运算符。


0

我也看了这个YouTube视频,遇到了同样的问题。 我是Python新手,请多包涵。 视频中的讲师们使用的是Python3,而我使用的是Python2, 所以我不确定所有的语法是否相同。但是通过使用本主题中找到的一些信息和来自此链接的信息:(http://www.wikihow.com/Calculate-Mortgage-Payments),我成功地得到了答案。(我的计算结果与在线抵押贷款计算器相匹配)

#!/usr/bin/env python
# The formula I used:
M = L * ((I * ((1+I) ** n)) / ((1+I) ** n - 1))

# My code (probably not very eloquent but it worked)

# monthly payment
M = None
# loan_amount
L = None
# interest rate
I = None
# number of payments
n = None

L = raw_input("Enter loan amount: ")
L = float(L)
print(L)

I = raw_input("Enter interest rate: ")
I = float(I)/100/12
print(I)

n = raw_input("Enter number of payments: ")
n = float(n)
print(n)

M = L * ((I * ((1+I) ** n)) / ((1+I) ** n - 1))

M = str(M)
print("\n")
print("Monthly payment is " + M)

为什么你在使用Python 2而不是Python 3?如果你不需要支持遗留代码库的话,使用Python 3似乎更好。 - Paul
将利率除以12可能是错误的,这取决于用户输入的是APR还是APY。https://www.reference.com/business-finance/simple-explanation-apr-interest-617c3e5d7894f8ed?qo=contentSimilarQuestions - Andrew Wagner

-2

我也遇到了这个问题,以下是我的解决方案

loan = input('Enter Loan Amount: ')
loan = float(loan)

numberOfPayments = input('Enter Loan payments in years: ')
numberOfPayments = float(numberOfPayments) * 12

interest = input('Annuel interest Rate: ')
interest = float(interest)/100/12

monthlyPayments = loan * (interest * (1 + interest) ** numberOfPayments) / ((1 + interest) ** numberOfPayments - 1)

print (round(monthlyPayments))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接