【问题标题】:NameError: name 'mNewCal' is not defined issueNameError:名称“mNewCal”未定义问题
【发布时间】:2021-02-28 00:13:42
【问题描述】:

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

if mNewCal or fNewCal < 1200:

NameError:未定义名称“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.")

【问题讨论】:

    标签: python error-handling nameerror


    【解决方案1】:

    您只定义 fNewCalmNewCal 并在 or 语句中同时拥有两者,如果您尚未定义其中一个变量并且由 if 语句检查,它将引发 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.")
    

    一般来说,既然您已经有了一个gender 变量,那么也没有理由对您的其他变量进行性别处理,尽管其余变量目前不会破坏程序,因为它们都取决于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:
        BMR = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age)
    elif gender == 2:
        BMR = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)
    
    if gender == 1:
        if exercise == 1:
            cal = BMR * 1.2
    
        elif exercise == 2:
            cal = BMR * 1.375
    
        elif exercise == 3:
            cal = BMR * 1.55
    
        elif exercise == 4:
            cal = BMR * 1.8
    
    else:
        if exercise == 1:
            cal = BMR * 1.2
    
        elif exercise == 2:
            cal = BMR * 1.375
    
        elif exercise == 3:
            cal = BMR * 1.55
    
        elif exercise == 4:
            cal = BMR * 1.8
    
    if gender == 1:
        TotalCal = BMR * 1.2
        # print(mTotalCal)
    
    else:
        TotalCal = BMR * 1.2
    # print(fTotalCal)
    
    
    loseWeight = str(input("do you want to loose weight? if yes, enter Y: \n if no enter N: \n "))
    
    if loseWeight == "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 = TotalCal - 500
    else:
        newCal = TotalCal - 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.")
    

    【讨论】:

    • 一旦你修复了变量名,你就可以从大多数条件块中去掉很多重复的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2021-04-15
    • 2019-01-26
    • 2021-10-05
    • 2017-08-16
    相关资源
    最近更新 更多