它指向小于号,显示语法无效

-3 投票
2 回答
99 浏览
提问于 2025-04-14 18:23
File "main.py", line 4
    if person_weight >= 18.5 or <= 25:
                            ^
SyntaxError: invalid syntax


** Process exited - Return Code: 1 **
Press Enter to exit terminal

事情是这样的:

weight = float(input("What is your weight:", ))
height = float(input("How tall in inches are you:", ))
BMI = weight * 703/height
if person_weight >= 18.5 or <= 25:
    print("You have optimal weight!")
elif person_weight < 18.5:
    print("You are underweight, start eating more!")
elif person_weight > 25:
    print("You are OVERWEIGHT do something with your life!")

我试着把18.5和25放在对面的地方,但这样做没有成功。

2 个回答

0

你没有定义 person_weight 这个变量。可能你想提到的是 BMI(身体质量指数)。在 if 语句中,你应该写成 if 18.5 <= BMI <= 25: 或者 if BMI >= 18.5 or BMI <= 25: 这样的形式。

weight = float(input("What is your weight:", ))
height = float(input("How tall in inches are you:", ))
BMI = weight * 703/height
if BMI >= 18.5 or BMI <= 25:
    print("You have optimal weight!")
elif BMI < 18.5:
    print("You are underweight, start eating more!")
elif BMI > 25:
    print("You are OVERWEIGHT do something with your life!")
2
if person_weight >= 18.5 or <= 25:

这是一个语法错误。

在日常的英语对话中,当你说“如果x大于y或者小于z”,大家都明白你是在说x在这两个比较中。

但是在Python中,情况并不是这样。你必须在每个比较中明确写出所有的变量:

if person_weight >= 18.5 or person_weight <= 25:

不过我觉得这个比较没有意义,因为它永远都是对的。你能想到的每个数字要么大于等于18.5,要么小于等于25。

也许你是想用“与”的条件,而不是“或”的条件?你是不是想检查它是否在这两个数字之间?

撰写回答