【问题标题】:Calculating mortgage interest in Python用 Python 计算抵押贷款利息
【发布时间】: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


【解决方案1】:

显然你把公式复制错了。

wrong:   * numberOfPayments 

correct: ** numberOfPayments 

注意:它在公式中出现两次 注意:在 python 中,** 是“幂”运算符。

【讨论】:

    【解决方案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))
    

    【讨论】:

      【解决方案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)
      

      【讨论】:

      • 你为什么使用 Python 2 而不是 Python 3?如果您不必支持旧代码库,似乎使用 Python 3 会更好。
      • 将利率除以 12 可能是错误的,具体取决于用户输入的是 APR 还是 APY。 reference.com/business-finance/…
      【解决方案4】:

      借助示例,这就是我所做的。

      # 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】:

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

        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)
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-12-13
            • 2023-04-05
            • 2019-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-10
            • 2012-05-31
            相关资源
            最近更新 更多