简体   繁体   中英

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. So you're ending up with (0 * 0) - principal. 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. You're overwriting their values when you do that. Just use them directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM