[英]Multiple parameters definition of a function Python
I'm trying to write a function that calculates the cost of a loan, but I keep getting the cost of the loan to be the negative value of what the user inputs as the amount of the loan. 我正在尝试编写一个计算贷款成本的函数,但我一直使贷款成本成为用户输入的贷款金额的负值。
#define monthly payment
def MonthlyPayment (ammountOfLoan, numberOfPeriods,yearlyInterestRate):
ammountOfLoan = 0
numberOfPeriods = 0
yearlyInterestRate = 0
payment = [(yearlyInterestRate/12)/(1-(1+yearlyInterestRate/12))**(-numberOfPeriods)] * ammountOfLoan
return (payment)
#define cost of loan
def LoanCost(principal, month, payment):
period = 0
month = 0
payment = 0
cost = period * payment - principal
return (cost)
#calculate cost of loan
def main():
loan = float(raw_input("What is the ammount of your loan? "))
period = float(raw_input("How many months will you make payments? "))
rate = float(raw_input("What is the interest rate? "))
rate = rate / 100
MonthlyPayment(loan, period, rate)
costOfLoan = LoanCost(loan, period, rate)
print "The cost of the loan is $" + str(costOfLoan)
#run main
main()
LoanCost is setting period and payment to 0 (you're making the same mistake in MonthlyPayment, as well), then multiplying them. LoanCost会将期间和付款设置为0(您在MonthlyPayment中也犯了同样的错误),然后将它们相乘。 So you're ending up with (0 * 0) - principal.
因此,您最终得到(0 * 0)-主体。 You're also calling the second parameter "month", when what you really mean is "period".
当您真正表示的是“期间”时,您也将第二个参数称为“月”。
Just to clarify, when you have a function definition like 澄清一下,当您有一个函数定义
def func(a, b, c):
You shouldn't initialize a, b, and c to zero inside the function body. 您不应在函数体内将a,b和c初始化为零。 You're overwriting their values when you do that.
这样做会覆盖它们的值。 Just use them directly.
只需直接使用它们即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.