语法错误无效语法。不知道为什么
height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))
bmi = weight / height ** 2
bmi2 = int(bmi)
if bmi2 < 18.5:
print(f"Your BMI is {bmi2}, you are underweight")
elif bmi2 > 18.5 < 25:
print(f"Your BMI is {bmi2}, you are normal weight")
elif bmi2 > 25 < 30:
print(f"Your BMI is {bmi2}, you are slightly overweight")
elif bmi2 > 30 < 35:
print(f"Your BMI is {bmi2}, you are obese")
else:
print(f"Your BMI is {bmi2}, you are cliniclly obese")
它说“elif bmi2 > 18.5 < 25:”这部分的语法错误无效语法,但我不知道为什么
height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))
bmi = weight / height ** 2
bmi2 = int(bmi)
if bmi2 < 18.5:
print(f"Your BMI is {bmi2}, you are underweight")
elif bmi2 > 18.5 < 25:
print(f"Your BMI is {bmi2}, you are normal weight")
elif bmi2 > 25 < 30:
print(f"Your BMI is {bmi2}, you are slightly overweight")
elif bmi2 > 30 < 35:
print(f"Your BMI is {bmi2}, you are obese")
else:
print(f"Your BMI is {bmi2}, you are cliniclly obese")
it says Syntax Error invalid syntax on "elif bmi2 > 18.5 < 25:" this part but I have no idea why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是缩进。稍后我还有其他一些事情。
这就是您的脚本以使其工作的方式:
如果和
else
条件应与行的开头一致。只有在有嵌套的陈述(例如,依此类推,依此类推)时,才会缩进内部。您的脚本没有该脚本,因此不需要在语句上有任何缩进。
以这种格式大于或低于或小的语法:
将条件设置为大于价值的条件,但小于值二。实际上,使用它的方式,它实际上是在第一个情况下设置条件,因为BMI大于18.5和18.5小于25。这将导致每个
bmi2
超过18.5会导致正常体重,因为无论BMI2
在这种情况下,第二个条件的第二种条件少于25,无论人们进入什么都会始终是正确的。为了准确性,我还将第二个语句中的
少于25
更改为小于或等于24.99
,大于25
更大大于24.99
和小于30
to小于或等于第三个语句中的29.99
,最后,大于30
大于或等于30
和小于35
to小于或等于第三个语句中的34.99
,因为这与范围从实际的BMI计算器和prvents来自重叠值的其他偶然性。既然重要的东西已经涵盖了,我还进行了一些化妆品的更改,包括在变量声明之间添加空白,并使用逗号和报价将变量
bmi2
与文本分开。这使得判断哪一个是谁和正在发生的事情变得容易一些。The issue is indentation. There are also a few other things that I'll explain later.
This is how your script should look in order for it to work:
All of the
if
andelse
conditions should be aligned to the beginning of the line. One would only indent inside if there were a nested statement such asAnd so on and so forth. Your script doesn't have that so there doesn't need to be any indentation on the statements.
The syntax for greater than or less than should be in this format:
That sets the condition as being greater than value one but less than value two. With the way that you have it, it is, in fact, setting the condition in the first one for example, as bmi being greater than 18.5 and 18.5 being less than 25. This will cause every
bmi2
that's over 18.5 to be result in normal weight because no matter whatbmi2
is in that case, the second condition of 18.5 being less than 25 will always be true no matter what one enters.For accuracy, I also changed
less than 25
in the second statement toless than or equal to 24.99
,greater than 25
togreater than 24.99
andless than 30
toless than or equal to 29.99
in the third statement, and finally,greater than 30
togreater than or equal to 30
andless than 35
toless than or equal to 34.99
in the third statement as that lines up with the ranges from the actual BMI calculator and prvents other wonkiness from overlapping values.Now that the important things have been covered, I also made some cosmetic changes including adding a blank line between the variable declarations and using commas and quotes to separate the variable
bmi2
from the text. This makes it a bit easier to tell which one is who and what's going on.