0

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()
1
  • why would you pass in arguments if you're just going to immediately discard their values? (e.g. by doing numberOfPeriods = 0). you should really go over the basics of python. just read a tutorial, docs, learn python the hard way, code academy, anything. Commented Mar 28, 2014 at 17:36

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.