+ 1
What's wrong with this BMI calculator?
x = int(input()) x2 = float(input()) x3 = x/x2**2 if x3 < 18.5: print('Underweight') if x3 > 18.5 and x3 < 24.9: print('Normal') if x3 > 24.9 and x3 < 29.9: print('Overweight') if x3 > 29.9: print('Obesity') It passes 4 of the 5 tests, I thought it was some technicality (some misspelled word) but after reviewing everything is fine, and should pass all 5 tests. Help please
4 Respostas
+ 4
x = int(input())
x2 = float(input())
x3 = x/x2**2
if x3 <= 18.5:
print('Underweight')
elif x3 > 18.5 and x3 <=25:
print('Normal')
elif x3 > 25 and x3 <= 30:
print('Overweight')
elif x3 > 30:
print('Obesity')
It passes 4 of the 5 tests, I thought it was some technicality (some misspelled word) but after reviewing everything is fine, and should pass all 5 tests. Help please
*** Note: your code is failing because if you enter 24.9<=bmi<=25, you will get no output. For example, 24.95, 29.99, 18.5 all get excluded.
elif is needed as you don't want all the if statements to run once you find a solution.
Read more about relational operators (comparison operators) and if elif statements
+ 4
Neiro Rincon ,
your code sample is not easy to read, because the variable names are x, x2 and x3.
=> why not using meaningful names like 'weight', 'height' and 'bmi' ?
your "naming conventions" may not be an issue for you in a code in this small size, but is a nightmare in real applications.
0
It will be a good idea to use
if
#your code
elif
#your code
else
#your code
0
Try this,
weight= float(input())
height= float(input())
x= weight/(height**2)
if x>0 and x<=18.5:
print("Underweight")
elif x>=18.5 and x<=25:
print("Normal")
elif x>=25 and x<=30:
print("Overweight")
elif x>30:
print("Obesity")