Python control Flow

Hi, I am a new member here, I am going through the project medical insurance in python control flow. while analyzing bmi using if, elif, and else statement, Hint shows the code written below:
def analyze_bmi(bmi_value):
if bmi_value > 30:
print(“Your BMI is in the obese range. To lower your cost, you should significantly lower your BMI.”)
elif bmi_value >= 25 and bmi_value <= 30:
print(“Your BMI is in the overweight range. To lower your cost, you should lower your BMI.”)
elif bmi_value >= 18.5 and bmi_value < 25:
print(“Your BMI is in a healthy range.”)
else:
print(“Your BMI is in the underweight range. Increasing your BMI will not help lower your cost, but it will help improve your health.”)

where as to my understanding the purpose of using the elif statement is that we don’t need to specify the upper range. I was wondering if the code below has something wrong as compared to code above:

def analyze_bmi(bmi_value):
if bmi_value >30:
print(“Your BMI is in the obese range. To lower your cost, you should significantly lower your BMI.”)
elif bmi_value >= 25:
print(“Your BMI is in the overweight range. To lower your cost, you should lower your BMI.”)
elif bmi_value>=18.5:
print(“Your BMI is in a healthy range.”)
else:
print (“Your BMI is in the underweight range. Increasing your BMI will not help lower your cost, but it will help improve your health.”)

Please suggest.

To be honest, the second example is more along the path of my own reasoning and the first example just makes me cringe every time I see something like that. The first example screams of naivete and lack of understanding.

2 Likes

BOTH ARE CORRECT!
But the main point here is to make you understand the different approaches with which one can solve the problem. Being a programmer you need to be open minded towards all possible solutions that exist for a particular problem.
I Hope you understand.
Happy Coding :smiley:

Thanks a lot for the feedback

2 Likes

Thank you for the feedback

1 Like