'I am trying to code a BMI Calculator, the code is finished but it gives error, please help me to find the bug and fix it [closed]
BMI = 0
weight = int(input()) # weight in kg
height = float(input()) # height in m
BMI = weight / height ** 2
if BMI < 18.5:
print("Underweight")
elif BMI <= 18.5 and BMI > 25:
print("Normal")
elif BMI >= 25 and BMI < 30:
print("Overweight")
else:
print("Obesity")
Solution 1:[1]
Your first elif statement
elif BMI <= 18.5 and BMI > 25:
needs to be changed like this
elif BMI >= 18.5 and BMI < 25:
Solution 2:[2]
I think answer by @python-learner is the one you want (https://stackoverflow.com/a/71501575/218663) but I want to show you an alternative that I use frequently to make these kind of range rests easier. Let's use a function to get the proper text. Then we can rely on the ability to return from this function that makes our range tests easier to follow:
def bmi_result(bmi):
if bmi < 18.5:
return "Underweight"
if bmi < 25:
return "Normal"
if bmi < 30:
return "Overweight"
return "Obesity"
Drop a comment on this answer so I know you got it and I will delete it. It was a little too much to put in a comment to your question and the answer you want is again by @python-learner
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Python learner |
| Solution 2 | JonSG |
