这是代码:
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")
Run Code Online (Sandbox Code Playgroud)
在elif BMI> = 18.5和<25.0行获取无效语法:
>,<其余是二元运算符.它正在寻找每一侧的操作数,当它在左边找到一个关键字时会and < 25.0抛出一个SyntaxError.
通常的方法是:
if BMI >= 18.5 and BMI < 25.0:
Run Code Online (Sandbox Code Playgroud)
但是不平等有一条捷径:
if BMT < 18.5:
# underweight
elif 18.5 <= BMI < 25.0:
# normal
elif 25.0 <= BMI < 30:
# overweight
elif 30 <= BMI:
# super overweight
Run Code Online (Sandbox Code Playgroud)