簡體   English   中英

為什么我的 BMI 代碼顯示錯誤的類別? 例如,當我得到 BMI = 21.7 但它會打印 OVERWEIGHT 語句

[英]Why my BMI code shows the wrong categories? For example when I get BMI = 21.7 but it prints the OVERWEIGHT statement

Weight = float(input("Enter your weight (kg) : "))
Height = float(input("Enter your height (cm): "))
Height_M = float(Height/100)
BMI = float(Weight/Height_M**2)
BMI_R = round(BMI,1)
if BMI_R < 18.5:
    print(f"Your BMI is {BMI_R} and you are UNDERWEIGHT.")
elif(18.5 > BMI_R < 24.9):
    print(f"Your BMI is {BMI_R} and you are NORMAL.")
elif(25 > BMI_R < 29.9):
    print(f"Your BMI is {BMI_R} and you are OVERWEIGHT.")
elif BMI_R > 30:
    print(f"Your BMI is {BMI_R} and you are OBESE.")

[參考][1]

示例[1]: https://i.stack.imgur.com/CQBGJ.png

Weight = float(input("Enter your weight (kg) : "))
Height = float(input("Enter your height (cm): "))
Height_M = float(Height/100)
BMI = float(Weight/Height_M**2)
BMI_R = round(BMI,1)
BMI_R = 21.7
if BMI_R < 18.5:
  print(f"Your BMI is {BMI_R} and you are UNDERWEIGHT.")
elif(18.5 < BMI_R < 24.9):
  print(f"Your BMI is {BMI_R} and you are NORMAL.")
elif(25 < BMI_R < 29.9):
  print(f"Your BMI is {BMI_R} and you are OVERWEIGHT.")
elif BMI_R > 30:
    print(f"Your BMI is {BMI_R} and you are OBESE.")

18.5 > BMI_R < 24.9 --> 18.5 < BMI_R < 24.9

25 < BMI_R < 29.9 --> 25 < BMI_R < 29.9

您添加了>而不是< 請仔細看。

您對正常和超重的范圍比較是錯誤的。 這兩種情況的左比較運算符需要是“<”。

你的條件不對。 您可以嘗試以下條件。

if BMI_R < 18.5:
    print(f"Your BMI is {BMI_R} and you are UNDERWEIGHT.")
elif(18.5 < BMI_R < 24.9):
    print(f"Your BMI is {BMI_R} and you are NORMAL.")
elif(25 < BMI_R< 29.9):
    print(f"Your BMI is {BMI_R} and you are OVERWEIGHT.")
elif BMI_R > 30:
    print(f"Your BMI is {BMI_R} and you are OBESE.")

甚至你可以寫出這些條件。

if BMI_R < 18.5:
    print(f"Your BMI is {BMI_R} and you are UNDERWEIGHT.")
elif(BMI_R < 24.9):
    print(f"Your BMI is {BMI_R} and you are NORMAL.")
elif(BMI_R< 29.9):
    print(f"Your BMI is {BMI_R} and you are OVERWEIGHT.")
elif BMI_R > 30:
    print(f"Your BMI is {BMI_R} and you are OBESE.")

第二段代碼似乎更聰明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM