这是我在YouTube上用Python编程教程从零开始到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)
我对以下代码使用了类似的[post's][1]答案:
相关问题 更多 >
编程相关推荐