2016-06-30 4 views
1

Dies ist ein Code, den ich von der Zero to Hero mit Python-Programmierung Tutorial von YouTube bekam, um eine Hypothek zu berechnen. Ich versuche zu verstehen, warum dieser Code mir nicht die gleichen Antworten gibt, die ich mit anderen Zahlungsrechnern bekomme. Ich weiß, es gibt auch andere Codes, die mir die richtige Antwort geben, aber ich versuche, herauszufinden, was mit diesem falsch ...Youtube Python Mortgage Calculator (null zu Held)

# 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

Sie müssen sagen, was damit nicht stimmt. Was ist das Ergebnis? Was sollte es sein? Was sind die Eingabewerte? –

+0

Ich bin mir ziemlich sicher, dass das nicht die richtige Formel ist, siehe: https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula Beachten Sie, dass Sie nicht mit n multiplizieren, sondern n als Exponent verwenden. Auch, was es wert ist, würde ich sehr vorsichtig von einem Tutorial sein, das Ihnen sagt, "Python-Variablen wie diese zu deklarieren und zu initialisieren". Erstens gibt es in Python keine solche Sache, es gibt nur eine Variablenzuweisung. Während es nützlich sein kann, einen Wert zuzuweisen, bevor Sie etwas mit einer Variablen machen, ist Ihr Code in diesem Fall nicht betroffen, und Sie können diese Zeilen löschen und die gleichen Ergebnisse erhalten. –

Antwort

0

habe ich ein ähnliches [Post] [1] Antwort für die folgender code:

# 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

Sie sollten es als Wiederholung markiert haben, dann. –

+0

Ja, könnte das getan haben. Scheint so, als ob die Angabe des genauen Codes sauberer wäre. – jsh