YouTube Python抵押计算器(零到英雄)

2024-05-16 02:56:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在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)

Tags: theto代码inputpaymentfloatwillloan
1条回答
网友
1楼 · 发布于 2024-05-16 02:56:11

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

相关问题 更多 >