用Python计算抵押利息

2024-05-15 16:21:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在通过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英镑。如果能帮助我理解这个方程式,我将不胜感激。


Tags: ofthetoinputratefloatoutprint
3条回答

我也看了这段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)

显然你把公式抄错了。

wrong:   * numberOfPayments 

correct: ** numberOfPayments 

注:在公式中出现两次 注意:在python中,**是“to the power of”操作符。

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

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

相关问题 更多 >