2016-06-30 18 views
1

これは私がモーゲージを計算するためにYouTubeからPythonプログラミングチュートリアルでゼロからヒーローを外したコードです。私はこのコードが他の支払い計算機を使っているのと同じ回答を私に与えていない理由を理解しようとしています。私はのために同様の[投稿の] [1]の答えを使用YouTube Python Mortgage Calculator(ゼロからヒーロー)

# M = L[i(1+i)n]/[(1+i)n-1] 


#Declare and initialize the variables 
monthlyPayment = 0 
loanAmount = 0 
interestRate = 0 
numberOfPayments = 0 
loanDurationInYears = 0 

#Ask the user for the values needed to calculate the monthly payments 
strLoanAmount = input("How much money will you borrow? ") 
strInterestRate = input("What is the interest rate on the loan? ") 
strLoanDurationInYears = input("How many years will it take you to pay off the loan? ") 

#Convert the strings into floating numbers so we can use them in the formula 
loanDurationInYears = float(strLoanDurationInYears) 
loanAmount = float(strLoanAmount) 
interestRate = float(strInterestRate) 

#Since payments are once per month, number of payments is number of years for the loan * 12 
numberOfPayments = loanDurationInYears*12 

#Calculate the monthly payment based on the formula 
monthlyPayment = loanAmount * interestRate * (1+ interestRate) * numberOfPayments \ 
    /((1 + interestRate) * numberOfPayments -1) 

#provide the result to the user 
print("Your monthly payment will be " + str(monthlyPayment)) 

#Extra credit 
print("Your monthly payment will be $%.2f" % monthlyPayment) 
+0

あなたは何が間違っているかを伝える必要があります。結果は何ですか?それは何でしょうか?入力値は何ですか? –

+0

私は正しい公式ではないと確信しています。https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula nを掛け合わせるのではなく、指数としてnを使用することに注意してください。また、それが価値あるものであれば、Python変数を宣言して初期化するチュートリアルには非常に注意が必要です。まず、Pythonにはこのようなことはなく、変数の代入しかありません。変数を使用する前に値に代入すると便利ですが、この場合はコードに影響はなく、これらの行を削除して同じ結果を得ることができます。 –

答えて

0

...私は私に正しい答えを与える他のコードがある知っているが、私はこの1つが悪いのかを把握しようとしています次のコード:

# M = L[i(1+i)n]/[(1+i)n-1] 


#Declare and initialize the variables 
monthlyPayment = 0.0 
loanAmount = 0.0 
interestRate = 0.0 
numberOfPayments = 0.0 
loanDurationInYears = 0.0 

#Ask the user for the values needed to calculate the monthly payments 
strLoanAmount = input("How much money will you borrow? ") 
strInterestRate = input("What is the interest rate on the loan? ") 
strLoanDurationInYears = input("How many years will it take you to pay off the loan? ") 

#Convert the strings into floating numbers so we can use them in the formula 
loanDurationInYears = float(strLoanDurationInYears) 
loanAmount = float(strLoanAmount) 
interestRate = float(strInterestRate)/100/12 

#Since payments are once per month, number of payments is number of years for the loan * 12 
numberOfPayments = float(loanDurationInYears)*12 

#Calculate the monthly payment based on the formula 
monthlyPayment = loanAmount * (interestRate * (1 + interestRate) ** numberOfPayments)/((1 + interestRate) ** numberOfPayments - 1) 

#provide the result to the user 
print("Your monthly payment will be " + str(monthlyPayment)) 

#Extra credit 
print("Your monthly payment will be $%.2f" % monthlyPayment) 


    [1]: http://stackoverflow.com/questions/29804843/formula-for-calculating-interest-python 
+0

リピートとしてフラグを立てておく必要があります。 –

+0

うん、それをしたかもしれない。正確なコードを与えるのがきれいだったようです。 – jsh