错误:分配前已引用变量-为什么?

时间:2018-06-26 16:26:39

标签: python python-3.x

我不断收到以下信息:

protein = macros_p / 4
  

UnboundLocalError:分配前已引用本地变量macros_p

函数`(goal,body_type,daily_exp)的参数不是问题,因为我已经在以前的函数中使用了它们而没有问题,但是这个没有用,并且严重困扰了我。

有人可以解释吗?

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 个答案:

没有答案