Python 3.3:以下代码的第7行中的语法错误无效

时间:2015-02-18 02:25:05

标签: python

以下是代码:

Weight = float(input("Enter weight in Kilograms: "))
Height = float(input("Enter height in meters: "))
BMI = (Weight / (Height**2))
print ("%.2f" %BMI)
if BMI < 18.5:
    print ("You are under weight")
elif BMI >= 18.5 and < 25.0:
    print ("You weight is normal")
elif BMI >= 25.0 and < 30.0:
    print ("You are overweight")
elif BMI >= 30.0:
    print ("You are overweight")

在该行获取无效语法 elif BMI> = 18.5且&lt; 25.0:

1 个答案:

答案 0 :(得分:5)

><,其余为二元运算符。它正在寻找每一方的操作数,当它在左侧找到关键字and < 25.0时会抛出SyntaxError

执行此操作的常规方法是:

if BMI >= 18.5 and BMI < 25.0:

但不平等有一条捷径:

if BMT < 18.5:
    # underweight
elif 18.5 <= BMI < 25.0:
    # normal
elif 25.0 <= BMI < 30:
    # overweight
elif 30 <= BMI:
    # super overweight