YouTube Python按揭计算器(零至英雄)

时间:2016-06-30 04:08:48

标签: python youtube

这是一个代码,我在YouTube上使用Python编程教程从Zero to Hero获得计算抵押贷款的代码。我试图理解为什么这段代码没有给我相同的答案我正在使用其他付款计算器。我知道还有其他代码可以给我正确的答案,但我想弄清楚这个有什么问题......

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

1 个答案:

答案 0 :(得分:0)

我对以下代码使用了类似的[post] [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