0
Python,The test case gives wrong? But the code is working please can anyone tell me what is the problem?
# BMI Calculation w=float(input()) h=float(input()) bmi=w/(h*h) if bmi<18.5: print ("Underweight") elif 18.5<=bmi<25: print ("Normal") elif 25<=bmi<30: print("Overweight") elif 30<=bmi: print ("Obesity") print (bmi)
12 Answers
+ 1
BroFar
If we take a look at Normal case, a BMI "more or equal to 18.5 and less than 25" means that :
BMI >= 18.5 and BMI < 25
<=> 25 > BMI >= 18.5
<=> 18.5 <= BMI < 25
There are three equivalent ways to write the same comparison.
Unless there is one more pythonic than the others, the three of them are valid.
Please test the code presented.
+ 4
I don't think you have to print the BMI at the end, to pass the test cases.
Next time please use relevant tags, like "Python for beginners", "BMI calculator", "Code Project", etc.
+ 2
BroFar
Yes, it's another way to do it. Not the simplest nor the more pythonic though.
+ 1
Hamza Ahmed looks like you are confused about greater than and less than. Please look at your code script closely.
+ 1
Delorme
There actually is a problem with the signs, notice that it in the nirmal definitin it says 18.5<= (less than or equal to) when it should be more than or equal to. This is the same with the overweight and obesity definition.
+ 1
Kamil Hamid
Here are the instructions from the BMI calculator code project in Python for beginners :
"The resulting number indicates one of the following categories:
Underweight = less than 18.5
Normal = more or equal to 18.5 and less than 25
Overweight = more or equal to 25 and less than 30
Obesity = 30 or more"
I passed the test cases by copying the code and removing the extra print.
+ 1
Delorme
# BMI Calculation
w = int(input())
h = int(input())
bmi = w/(h*h)
print(bmi)
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")
+ 1
Delorme, <= is less than or equal to when it should be more than, as you have said. Notice that this has been put in the code instead of >= for the normal overweight and obese lower bounds.
I do understand this problen has been solved however I wish that it is clear that these symbols are wrong.
+ 1
Kamil Hamid, Ejeh Joseph,
I understand your rigorous points of view about how the human instructions have to be translated into code. However, as
BMI >= 18.5 is equivalent to
18.5 <= BMI,
whatever option you choose the comparison is the same. Here there is no right or wrong use of those symbols, there's no clear injunction to use a particular symbol, as long as the bounds are well defined. To me, choosing one or the other is about the part of freedom, of creativity a coder has although he's following guidelines.
Moreover, writing a<b<c is simpler and more pythonic than writing ((a<b) and (b<c)). As states the zen of Python : simple is better than complex, readability counts, etc.
Finally, the code above didn't have an issue passing the test cases for that (too) precise reason of writing things up.
0
BroFar As weird as they look at first glance, the comparison operators are ok here.
0
Delorme
Normal is greater than or equal to 18.5 and less than 25 ..
and others are not directionally correct .. Please look at the code presented.
0
Hamza Ahmed Your conditional statements are not correct. From your elif statement... You used the < signs where you were supposed to use > sign
Use BroFar code...it's correct
But don't just copy and paste the code...try to understand it