user3471046
user3471046

Reputation: 5

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

Upvotes: 0

Views: 130

Answers (1)

dano
dano

Reputation: 94891

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.

Upvotes: 3

Related Questions