-3

I'm new to functions, so I'm not quite sure how or why this is happening and I do not know how t fix it. Can someone explain why this error keeps occuring?

def loan_payment(l):
    loan = float(input("Please enter how much you expend monthly on loan payments:"))
    return loan
def insurance_cost(i):
    insurance = float(input("Please enter how much you expend monthly on insurance:"))
    return insurance
def gas_cost(g):
    gas = float(input("Please enter how much you expend monthly on gas:"))
    return gas
def maitanence_cost(m):
    maitanence = float(input("Please enter how much you expend monthly on maintanence:"))
    return maitanence
def monthly_cost(l, i, g, m):
    monthly_expenses = float(l + i + g + m)

    print("You expend $"+format(monthly_expenses, '.2f')+" in a month.")
    return float(monthly_expenses)
def yearly_cost(monthly_cost):
    yearly_expenses = 12 * monthly_cost

    print("At your current monthly expenses, in a year you will have paid $"+format(yearly_expenses, '.2f')+".")
    return yearly_expenses
def main():
    loan_payment(l)
    insurance_cost(i)
    gas_cost(g)
    maitanence_cost(m)
    monthly_cost(l, i, g, m)
    yearly_cost(monthly_cost)
main()

This code returns the error:

line 24, in main loan_payment(l) NameError: name 'l' is not defined

asakryukin
  • 2,524
  • 1
  • 13
  • 14
Joe Bird
  • 21
  • 4
  • 1
    I would suggest this is a great opportunity for taking some time to think about meaningful variable names because 'l' and 'i' are not particularly meaningful, from my understanding. Right now, every variable you're passing to methods in the main method has no value, and it's only because l is first that it's throwing the problem. – Zooby Dec 01 '17 at 03:03
  • Hint: Where did you define `l` in `main`? You can't pass a variable that doesn't exist. – ShadowRanger Dec 01 '17 at 03:08
  • I don't understand where to define l though, because if I define it under main it returns the error line 24, in main loan_payment(l) UnboundLocalError: local variable 'l' referenced before assignment – Joe Bird Dec 01 '17 at 03:21

2 Answers2

0

I think you may have gotten your wires a bit crossed on how python passes by assignment, you wouldn't be the first, and here's a great question to help, as well as how to assign values and defining methods. As best as I can tell, your main method should look more like:

def main():
    l = user_in_loan_payment()
    i = user_in_insurance_cost()
    g = user_in_gas_cost()
    m = user_in_maintanence_cost()
    monthly_cost = calculate_monthly_cost(l, i, g, m)
    yearly_cost = calculate_yearly_cost(monthly_cost)

I changed the method names to start with "calculate" or "user_in" to make it a little more readable.

Zooby
  • 325
  • 1
  • 7
0

That's exactly what the error says: l is not defined, like any other variable. If I get it right you are trying to enter values in first 4 functions and then use them in the next two functions for calculations.

If that's true, your code should be as following:

def loan_payment():
    loan = float(input("Please enter how much you expend monthly on loan payments:"))
    return loan
def insurance_cost():
    insurance = float(input("Please enter how much you expend monthly on insurance:"))
    return insurance
def gas_cost():
    gas = float(input("Please enter how much you expend monthly on gas:"))
    return gas
def maitanence_cost():
    maitanence = float(input("Please enter how much you expend monthly on maintanence:"))
    return maitanence
def monthly_cost(l, i, g, m):
    monthly_expenses = float(l + i + g + m)

    print("You expend $"+format(monthly_expenses, '.2f')+" in a month.")
    return float(monthly_expenses)
def yearly_cost(monthly_cost):
    yearly_expenses = 12 * monthly_cost

    print("At your current monthly expenses, in a year you will have paid $"+format(yearly_expenses, '.2f')+".")
    return yearly_expenses
def main():
    l = loan_payment()
    i = insurance_cost()
    g = gas_cost()
    m = maitanence_cost()
    mc = monthly_cost(l, i, g, m)
    yearly_cost(mc)
main()
asakryukin
  • 2,524
  • 1
  • 13
  • 14