Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to better write the code to find the number between two numbers? It's not reading the 2nd or the 3rd elif statements

BMI_num = 21

if BMI_num <= 18.5:
    BMI_title = 'Underweight'
    print ('Results . . . ')
    print ('Your BMI is: ', BMI_num , '--' , BMI_title) 


elif 18.5 > BMI_num  >= 24.9:
    BMI_title = 'Normal'
    print ('Results . . . ')
    print ('Your BMI is: ', BMI_num , '--' , BMI_title) 


elif 25 >= BMI_num >= 29.9:
    BMI_title = 'Overweight'
    print ('Results . . . ')
    print ('Your BMI is: ', BMI_num , '--' , BMI_title) 


elif BMI_num < 30:
    BMI_title = 'Obese'
    print ('Results . . . ')
    print ('Your BMI is: ', BMI_num , '--' , BMI_title)

>Solution :

You need to replace > with <. Comparisons such as 18.5 > BMI_num >= 24.9 will always be False as no number can simultaneously be less than 18.5 and more than 24.9.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You also may want to consider that when BMI_num is 24.95 or 29.95 no code will be run.

Also you can reduce some code duplication by printing after.

if BMI_num <= 18.5:
    BMI_title = 'Underweight'
elif 18.5 < BMI_num  < 25:
    BMI_title = 'Normal'
elif 25 <= BMI_num <= 29.9:
    BMI_title = 'Overweight'
elif BMI_num < 30:
    BMI_title = 'Obese'

print ('Results . . . ')
print (f'Your BMI is: {BMI_num} -- {BMI_title}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading