Home > other >  Is there a way to better write the code to find the number between two numbers? It's not readin
Is there a way to better write the code to find the number between two numbers? It's not readin

Time:04-23

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)

CodePudding user response:

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.

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}')

CodePudding user response:

The comparator signs in the elif conditionals are wrong, as they are the wrong direction and also do not cover the boundary cases. I would also suggest to use a function and also drop the redundant print statements, as follows:

def log_bmi(BMI_num):
    if BMI_num <= 18.5:
        BMI_title = 'Underweight'
    elif 18.5 < BMI_num <= 25:
        BMI_title = 'Normal'
    elif 25 < BMI_num <= 30:
        BMI_title = 'Overweight'
    else:
        BMI_title = 'Obese'
    print ('Results . . . ')
    print ('Your BMI is: ', BMI_num , '--' , BMI_title)

CodePudding user response:

Your method is actually to see if a number is between two numbers completely fine. It is just that your range is wrong. For the second if statement, 18.5 > BMI_num >= 24.9 is never possible. 24.9 is not less than 18.5. I believe you meant

    elif 18.5 < BMI_num <= 24.9:
        # some code that runs because this elif is true.

For the third elif, 29.9 can't be less than or equal to 25. You should change it too

    elif 25 <= BMI_num <= 29.9:
        # some code that runs because this elif is true

The fourth and first if statements are fine.

You might want to change your fourth if statement to

    elif BMI_num >= 29.9
        # some code that runs because this elif is true

because the only way it would work with the previous statements is if BMI_num is > 29.9 and less than 30. It also includes 29.9 to 30, unlike doing

    elif BMI_num >= 30
        # some code that runs because this elif is true

It could also be

    else:
        """
        some code that runs because all the previous ones were 
        false
        """

Another method is

    if {variable_name} in range(x, y)

, but don't use the curly braces, but just the variable, which in this code is BMI_num, and x, y are the two numbers. The range is all numbers from x to y excluding y. Add/subtract one from either variable to get the right range

  • Related