计算Python中的抵押贷款利息

时间:2015-04-22 17:26:54

标签: 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)

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

6 个答案:

答案 0 :(得分:1)

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

var input= "123|ABC S|peter s@xyz.com!!234|George K|george.k@xyz.com"; 
var result1 = input.match(/([a-zA-Z0-9._+-]+ [a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
var result = input.match(/((\d*)+ [a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
document.write(result1 + "," + result);

答案 1 :(得分:0)

显然你错误地复制了公式。

wrong:   * numberOfPayments 

correct: ** numberOfPayments 

注意:它在公式中出现两次 注意:在python中,**是“强大的”操作符。

答案 2 :(得分:0)

我也观看了这个YouTube视频并遇到了同样的问题。 我是一个蟒蛇新手所以忍受我。 视频中的教师使用的是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)

答案 3 :(得分: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)

答案 4 :(得分:0)

mortgage软件包非常出色:

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

答案 5 :(得分:-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))