Why the output is showing all my if cases? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
29th Sep 2020, 6:03 PM
Abhijith Mulavana
Abhijith Mulavana - avatar
8 Answers
+ 5
Example using your second if if 15>=bmi<16: if bmi is 14, then both 15>=14 and 14<16 are True so the if is True. You need to change it to if 15<=bmi<16:
29th Sep 2020, 6:10 PM
Russ
Russ - avatar
+ 6
As Russ and RKK mentioned, you should change all of your if statements like: if ... <= bmi < ... ... elif ...
29th Sep 2020, 6:20 PM
Lothar
Lothar - avatar
+ 5
because all your if conditions are getting true. change your, if 15>=bmi<16: to if bmi>=15 and bmi<16: and all the others too.
29th Sep 2020, 6:08 PM
Rohit
+ 5
Abhijith Mulavana instead of using a bunch of if statements only, use if elif else
29th Sep 2020, 6:33 PM
Rohit
+ 5
if bmi < 15: ... elif bmi < 16: ... You won't need to check if bmi >= 15 because that will be implied by the first if return False.
29th Sep 2020, 6:36 PM
Russ
Russ - avatar
+ 3
Your conditions are always true because, for example, 15 >= bmi < 16 is basically the same as bmi <= 15 and bmi < 16, which is true for all numbers less than or equal to 15. If the computed bmi is, let's say 10, it will trigger all the other similar if statements because bmi is less than all of the numbers used in their conditions. Similar to what Russ said, if you want a condition in Python that checks if a number (bmi) is between two other numbers (15 and 16), you can write your condition like this: 15 < bmi < 16, which is the same as saying: 15 < bmi and bmi < 16 or bmi > 15 and bmi < 16 You can also make use of elif and else statements: if bmi < 15: ... elif bmi < 16: ... ... elif bmi < 40: ... else: ...
29th Sep 2020, 6:36 PM
Zerokles
Zerokles - avatar
+ 2
Thank you Russ , RKK ,Lothar for the help Is there anything to optimise in this code? With "if" only
29th Sep 2020, 6:31 PM
Abhijith Mulavana
Abhijith Mulavana - avatar
- 1
In html, When i use / for ending code it displays the same results as without / why?plz answer me also
1st Oct 2020, 9:24 AM
shubham Bhati