Python Forum
Issue with program not passing to other elif condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with program not passing to other elif condition
#1
Hello

I am new to this forum and programming in general.

I am trying to create BMI calculator. the issue is that no matter what I do the program stops only on the first elif statement (>=18.5 to >=24.9), even if the BMI is higher


code:
print('enter you weight kg')
weight=float(input())
print('enter you height meter')
height=float(input())
BMI=(weight/height**2)

print(BMI)


if BMI<=18.4:                                   
    print('You are underweight')
elif BMI>=18.5 or BMI<=24.9:
    print(' You have Normal weight')
elif BMI >=25 or BMI<=29.9:
    print('You are overweight')
elif BMI>=30 or BMI<=34.9:
    print('You are moderately obese')
elif BMI >=35 or BMI<=39.9:
    print('You are severely obese')
elif BMI>=40:
    print('You are very severely obese')
Thank you
Gribouillis write Nov-18-2021, 10:19 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Change all the or's to and

Please use the tag's when posting code.
Abdirahman likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
if BMI<=18.4:
print('You are underweight')
elif BMI>=18.5 or BMI<=24.9:  # These should be 'and', not 'or'
print(' You have Normal weight')
elif BMI >=25 or BMI<=29.9: # And if I am 24.937?  No.  <= 18.4; >18.4 and < 25; >= 25 and < 30 
print('You are overweight')
elif BMI>=30 or BMI<=34.9:
print('You are moderately obese')
elif BMI >=35 or BMI<=39.9:
print('You are severely obese')
elif BMI>=40:
print('You are very severely obese')
Reply
#4
In Python, you can do
if 30 <= BMI < 35:
    ...
supuflounder and Abdirahman like this post
Reply
#5
Thank you for pointing that out. I'm still learning Python. Key here is the code as shown has these huge gaps, such as between 2.9 and 3; ranges must be open at one end and closed at the other, and continuous in a construct such as the OP showed. No gaps in the range are permitted.
Reply
#6
"or" is wrong. Every number is less than 25 OR greater than 18.4. You could use "and", but there is no reason for this as it just adds code, complexity and increases the chance for errors such as "I have a bmi of 18.45, what am I? According to the original post you are nothing as this number falls in the gap between underweight and normal weight.

Instead of specifying ranges EXPLICITELY you should do so IMPLICITELY. If you write the comparisons in increasing or decreasing order you don't have to specify both ends of the range. This code has no gaps. Every possible BMI value is handled.
if BMI <= 18.4:                                   
    print('You are underweight')
elif BMI < 25:
    print(' You have Normal weight')
elif BMI < 30:
    print('You are overweight')
elif BMI < 35:
    print('You are moderately obese')
elif BMI < 40:
    print('You are severely obese')
else
    print('You are very severely obese')
Abdirahman likes this post
Reply
#7
Thank you all for the help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 727 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  else condition not called when if condition is false Sandz1286 10 5,844 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,899 Jun-01-2020, 06:00 PM
Last Post: penahuse
  How to write a script to execute a program need passing additional input? larkypython 2 2,520 Nov-23-2019, 04:38 AM
Last Post: larkypython
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,236 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Facing issue with pyautogui program after PC upgrade Utkarsh29 2 2,936 Jul-15-2019, 05:20 PM
Last Post: Utkarsh29
  if else condition issue mmaz67 4 2,861 Jul-18-2018, 09:02 AM
Last Post: gontajones
  input issue elif jge047 2 3,165 Nov-23-2017, 06:29 AM
Last Post: zykbee
  leap year program issue jashajmera 3 3,861 Feb-04-2017, 11:51 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020