Error: Variable referenced before assignment - WHY?

116 Views Asked by At

I keep getting the following:

protein = macros_p / 4

UnboundLocalError: local variable macros_p referenced before assignment

The arguments for the function `(goal, body_type, daily_exp) aren't an issue because I've used them in prior functions without a problem but this one isn't working and it's seriously stumping me.

Can anyone please explain?

def macros(goal, body_type, daily_exp):
    if goal == "L" and body_type == "Ectomorph":
        daily_exp = (daily_exp - 200)
        macros_p = (daily_exp * 0.25)
        macros_c = (daily_exp * 0.55)
        macros_f = (daily_exp * 0.2)
    elif goal == "L" and body_type == "Mesomorph":
        daily_exp = (daily_exp - 250)
        macros_p = (daily_exp * 0.4)
        macros_c = (daily_exp * 0.3)
        macros_f = (daily_exp * 0.3)
    elif goal == "L" and body_type == "Endomorph":
        daiy_exp = (daily_exp - 500)
        macros_p = (daily_exp * 0.5)
        macros_c = (daily_exp * 0.1)
        macros_f = (daily_exp * 0.4)

    protein = (macros_p / 4)
    carbs = (macros_c / 4)
    fat = (macros_f / 9)
    print("Daily intake: " + str(daily_exp) + " calories")
    print(str(protein) + "g Protein")
    print(str(carbs) + "g Carbohydrates")
    print(str(fat) + "g Fat")
0

There are 0 best solutions below