Think about the actual output of the following statement:
(b <= 18.5) + (b <= 25) + (b <=30)
if b
is 18, then all 3 will be true, so the output will be 3
- "Obese". If b
is 24, then the second and third condition will be true, so the output will be 2
- "Overweight". If b
is 29, then only the third condition will be true, so the output will be 1
- "Normal". If b
is 31, then none of them will be true, so the output will be 0
- "Underweight".
A solution would be to reverse your array:
['Obese', 'Overweight', 'Normal', 'Underweight'][(b <= 18.5) + (b <= 25) + (b <=30)]
Or, you can reverse your conditions, so that the original order would work:
['Underweight', 'Normal', 'Overweight' , 'Obese'][(18.5 <= b) + (25 <= b) + (30 <= b)]
In this case, if b
is 18, then (18.5 <= b)
is False
, (25 <= b)
is False
, and (30 <= b)
is False
, so the output is 0
, "Underweight". If b
is 24, then (18.5 <= b)
is True
, (25 <= b)
is False
, and (30 <= b)
is False
, so the output is 1
, "Normal". If b
is 29, then (18.5 <= b)
is True
, (25 <= b)
is True
, and (30 <= b)
is False
, so the output is 2
, "Overweight". Lastly, if b
is 31, then (18.5 <= b)
is True
, (25 <= b)
is True
, and (30 <= b)
is True
, so the output is 3
, "Obese".