函数Python的多个参数定义

时间:2014-03-28 16:42:45

标签: python function parameters

我试图编写一个计算贷款成本的函数,但我不断将贷款成本作为用户输入的负值作为贷款金额。

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

1 个答案:

答案 0 :(得分:3)

LoanCost将期间和付款设置为0(您在MonthlyPayment中也犯了同样的错误),然后将它们相乘。所以你最终得到(0 * 0) - 校长。你还要调用第二个参数“月”,当你真正的意思是“句号”时。

只是为了澄清,当你有一个像

这样的功能定义时
def func(a, b, c):

您不应该在函数体内初始化a,b和c为零。当你这样做时,你会覆盖他们的价值观。直接使用它们。