NameError: name 'mNewCal' 未定义问题

时间:2021-02-28 00:13:42

标签: python error-handling nameerror

我的代码有问题。我不确定它有什么问题。谢谢

if mNewCal or fNewCal < 1200:

NameError: name 'mNewCal' 未定义

对不起,如果格式有点奇怪,堆栈溢出让它变得奇怪。

gender = int(input("enter your gender as a number from the following \n Male: 1 \n Female: 2 \n " ))
height = int(input("Please enter your height in inches: "))
age = int(input("Please enter your age: "))
weight = int(input("Enter your weight in lbs: "))
exercise = int(input("How much exercise do you do during the week (enter number) \n little to no: 1 \n light: 2 \n moderate: 3 \n heavy: 4 \n  "))
if gender == 1:
   mBMR = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age)
elif gender == 2:
    fBMR = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)

if gender == 1:
    if exercise == 1:
        cal = mBMR * 1.2

    elif exercise == 2:
        cal = mBMR * 1.375

    elif exercise == 3:
        cal = mBMR * 1.55

    elif exercise == 4:
        cal = mBMR * 1.8

else:
    if exercise == 1:
        cal = fBMR * 1.2

    elif exercise == 2:
        cal = fBMR * 1.375

    elif exercise == 3:
        cal = fBMR * 1.55

    elif exercise == 4:
        cal = fBMR * 1.8



if gender == 1:
    mTotalCal = mBMR * 1.2
    #print(mTotalCal)

else:
    fTotalCal = fBMR * 1.2
   # print(fTotalCal)


looseWeight = str(input("do you want to loose weight? if yes, enter Y: \n if no enter N: \n "))

if looseWeight == "Y":
    yesWeight = int(input("How much weight do you want to loose (lbs) ? "))

else:
    print("thank you for using Nakul Industries health program!")


weeks = yesWeight
days = weeks * 7
months = days / 30


if gender == 1:
    mNewCal = mTotalCal - 500
else:
    fNewCal = fTotalCal - 500



if mNewCal or fNewCal < 1200:
    print("WARNING! your total intake will be less then 1200 calories, please consult a doctor before following this.")
    print("In order to lose " + yesWeight + " ,it will take " + weeks + " weeks " + "\n" + "or " + days + " days" + "\n or " + "approximately " + months + " months.")
else:
    print(
        "In order to lose " + yesWeight + " ,it will take " + weeks + " weeks " + "\n" + "or " + days + " days" + "\n or " + "approximately " + months + " months.")

1 个答案:

答案 0 :(得分:0)

你只定义了 fNewCalmNewCal 并且两者都在 or 语句中,如果你没有定义一个变量并且它被 if 语句检查,它会引发一个 {{1} } 像这样。您应该只使用一个变量,例如 NameError,并使用现有的 newCal 变量确定它是女性还是男性,如下所示:

gender

一般来说,由于您已经有一个 gender = int(input("enter your gender as a number from the following \n Male: 1 \n Female: 2 \n ")) height = int(input("Please enter your height in inches: ")) age = int(input("Please enter your age: ")) weight = int(input("Enter your weight in lbs: ")) exercise = int(input( "How much exercise do you do during the week (enter number) \n little to no: 1 \n light: 2 \n moderate: 3 \n heavy: 4 \n ")) if gender == 1: mBMR = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age) elif gender == 2: fBMR = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age) if gender == 1: if exercise == 1: cal = mBMR * 1.2 elif exercise == 2: cal = mBMR * 1.375 elif exercise == 3: cal = mBMR * 1.55 elif exercise == 4: cal = mBMR * 1.8 else: if exercise == 1: cal = fBMR * 1.2 elif exercise == 2: cal = fBMR * 1.375 elif exercise == 3: cal = fBMR * 1.55 elif exercise == 4: cal = fBMR * 1.8 if gender == 1: mTotalCal = mBMR * 1.2 # print(mTotalCal) else: fTotalCal = fBMR * 1.2 # print(fTotalCal) looseWeight = str(input("do you want to loose weight? if yes, enter Y: \n if no enter N: \n ")) if looseWeight == "Y": yesWeight = int(input("How much weight do you want to loose (lbs) ? ")) else: print("thank you for using Nakul Industries health program!") weeks = yesWeight days = weeks * 7 months = days / 30 if gender == 1: newCal = mTotalCal - 500 else: newCal = fTotalCal - 500 if newCal < 1200: print("WARNING! your total intake will be less then 1200 calories, please consult a doctor before following this.") print( "In order to lose " + yesWeight + " ,it will take " + weeks + " weeks " + "\n" + "or " + days + " days" + "\n or " + "approximately " + months + " months.") else: print( "In order to lose " + yesWeight + " ,it will take " + weeks + " weeks " + "\n" + "or " + days + " days" + "\n or " + "approximately " + months + " months.") 变量,因此也没有理由对您的其他变量进行性别化,尽管其余的变量目前不会破坏程序,因为它们都取决于 {{1} }.这里没有无意义的性别变量,它会以这种方式正常工作:

gender