gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:37:15 26 4
gpt4 key购买 nike

这是我在YouTube上使用Python编程教程从零到英雄计算抵押贷款的代码。我试图理解为什么这段代码没有像使用其他付款计算器那样给我同样的答案。我知道还有其他代码可以给我正确的答案,但是我正在尝试找出这个代码有什么问题...

# 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]答案:

# 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

关于python - YouTube Python抵押计算器(从零到英雄),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38113840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com