web-dev-qa-db-ja.com

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)

すべてが機能しますが、最後にスローする値は完全に間違っています... 1年間で金利が10%の£100のローンでは、月額0.83ポンドを支払う必要はありません。理解を助けるためにこの方程式に頭を抱えるのを手伝ってくれればどんなに助かります。

9
BeerHuntor

例の助けを借りて、これは私がやったことです。

# 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)
2
C. Ramsey

私もこの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)
0
bddmass

この 住宅ローン パッケージはうまく機能します:

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

どうやらあなたは間違った式をコピーしました。

wrong:   * numberOfPayments 

correct: ** numberOfPayments 

注:数式の注に2回出現します。Pythonでは、**は「べき乗」演算子です。

0
Alfista71

これはかなりうまくいきました。正確ではありません。ローン計算機は通常数日で行くので、私は怠惰なので数か月で行くのですが、ここでは、実用的であるほど正確な、私が書いた簡単なものを示します。

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