语法错误无效语法。不知道为什么

发布于 2025-01-20 09:15:53 字数 639 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2025-01-27 09:15:53

问题是缩进。稍后我还有其他一些事情。

这就是您的脚本以使其工作的方式:

#!/usr/bin/env python3

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  18.5 < bmi2 <= 24.99:
    print(f"Your BMI is",bmi2," you are normal weight")
elif 24.99 < bmi2 <= 29.99:
    print(f"Your BMI is ",bmi2," you are slightly overweight")
elif 30 <= bmi2 <= 34.99:
    print(f"Your BMI is ",bmi2," you are obese")
else:
    print(f"Your BMI is ",bmi2," you are clinically obese")

如果和else条件应与行的开头一致。只有在有嵌套的陈述(例如

if apple > 5;
    if orange > 3;

,依此类推,依此类推)时,才会缩进内部。您的脚本没有该脚本,因此不需要在语句上有任何缩进。

以这种格式大于或低于或小的语法:

elif  value < variable < value2:

将条件设置为大于价值的条件,但小于值二。实际上,使用它的方式,它实际上是在第一个情况下设置条件,因为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:

#!/usr/bin/env python3

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  18.5 < bmi2 <= 24.99:
    print(f"Your BMI is",bmi2," you are normal weight")
elif 24.99 < bmi2 <= 29.99:
    print(f"Your BMI is ",bmi2," you are slightly overweight")
elif 30 <= bmi2 <= 34.99:
    print(f"Your BMI is ",bmi2," you are obese")
else:
    print(f"Your BMI is ",bmi2," you are clinically obese")

All of the if and else conditions should be aligned to the beginning of the line. One would only indent inside if there were a nested statement such as

if apple > 5;
    if orange > 3;

And 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:

elif  value < variable < value2:

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 what bmi2 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 to less than or equal to 24.99, greater than 25 to greater than 24.99 and less than 30 to less than or equal to 29.99 in the third statement, and finally, greater than 30 to greater than or equal to 30 and less than 35 to less 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.

尹雨沫 2025-01-27 09:15:53
height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))

height_2 = height*height
BMI = weight/height_2
BMI2=round(BMI)
if BMI2 <=18.5:
    print(f"Your BMI is {BMI2}, you are underweight.")
elif 18.6< BMI2 <= 24.99:
    print(f"Your BMI is {BMI2}, you have a normal weight.")
elif 25< BMI2 <=29.99:
    print(f"Your BMI is {BMI2}, you are slightly overweight.")
elif 30 < BMI2 <= 34.99:
    print(f"Your BMI is {BMI2}, you are obese.")
elif BMI2 > 35:
    print(f"Your BMI is {BMI2}, you are clinically obese.")
else:
    print(f"Your BMI is {BMI2}, you are clinically obese.")
height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))

height_2 = height*height
BMI = weight/height_2
BMI2=round(BMI)
if BMI2 <=18.5:
    print(f"Your BMI is {BMI2}, you are underweight.")
elif 18.6< BMI2 <= 24.99:
    print(f"Your BMI is {BMI2}, you have a normal weight.")
elif 25< BMI2 <=29.99:
    print(f"Your BMI is {BMI2}, you are slightly overweight.")
elif 30 < BMI2 <= 34.99:
    print(f"Your BMI is {BMI2}, you are obese.")
elif BMI2 > 35:
    print(f"Your BMI is {BMI2}, you are clinically obese.")
else:
    print(f"Your BMI is {BMI2}, you are clinically obese.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文