''' Calculate the BMI of a person given their height and weight '''

def calcBMI( h, w ):
BMI = w / h**2
return BMI

def main():
height = float(raw_input("Enter height in metres: "))
weight = float(raw_input("Enter weight in kilos: "))
BMI = calcBMI(height, weight)
print "BMI = %0.1f" % BMI

if BMI < 19:
print " You are currently underweight"
elif BMI >= 19:
print " You are in a healthy weight range"
elif BMI <= 25:
print "You are still in a healthy weight range"
elif BMI > 25:
print " You are now overweight. Please consider seeing your doctor"
elif BMI < 30:
print "You are in the overweight weight range. Please see your doctor"
elif BMI >= 30:
print " You are obese. Please make some changes and see your doctor"
else:
print " Invalid selection"

main()

This is question i was given

A BMI of less than 19 is considered underweight. A BMI between 19 and 25 inclusive is considered in the healthy weight range. A BMI above 25 but below 30 is considered overweight, while a BMI of 30 and above is considered obese.
Modify the program so that it prints out an appropriate health advisory.

Im just not getting the right advisory


Any help would be great

Recommended Answers

All 5 Replies

Look at the attached picture: there should be only 3 tests for the value of BMI (instead of 6 in your program).

if BMI < 19:
    print " You are currently underweight"
elif BMI >= 19:
    print " You are in a healthy weight range"

You will never get past these two statements as they cover every possibility. Note also, that the second "elif BMI >= 19:" is not necessary as after the first if statement that is the only remaining possibility. Do this with functions instead as that will be easier to understand, then you can convert back to if statements if you want to.

def less_than_19():
    print " You are currently underweight"

def greater_than_19(BMI):
    if BMI <= 25:
        print "You are still in a healthy weight range"
        return
    elif BMI < 30:
        print "You are in the overweight range. Please see your doctor"
        return
    else:   ## has to be >= 30
        print " You are obese. Please make some changes and see your doctor"


if BMI < 19:
    less_than_19()
else:
    greater_than_19(BMI)

should be something like this:

#bla bla bla

    if BMI < 19:
        print " You are currently underweight"
    elif BMI >= 19 and BMI <= 25:
        print " You are in a healthy weight range"
    elif BMI > 25 and BMI < 30:
        print " You are now overweight. Please consider seeing your doctor"
    elif BMI >= 30:
        print " You are obese. Please make some changes and see your doctor"
    else:
        print " Invalid selection"

Please next time use code tags,- otherwise many people will ignore you. (i too ;) )

Generally speaking, we don't provide complete answers for homework problems, but hints to help the OP figure it out for themselves. Homework is meant to be a learning tool. And usually it is coded
elif 19 < BMI <= 25:
but is at the individual's preference of course.

Using the OP's original code, it would be something like the following, since an and statement is the same as nested if statements (both Gribouillis and I were hinting at separate blocks for each level, i.e. an additional indent).

if BMI < 19:
    print " You are currently underweight"
else:     
    ## can only be >= 19  ------------------------------------------
    if BMI <= 25:     ## else is the >= 19 
        print "You are still in a healthy weight range"
    else:     
        ## can only be > 25 ---------------------------------------
        if BMI < 30:
            print "You are in the overweight weight range. Please see your doctor"
        else:     
            ## can only be >= 30 ----------------------------------
            print " You are obese. Please make some changes and see your doctor

sorry i didnt want a complete answer i just want someone to point out what u was doing wrong. I just couldnt see it, needed a new set of eyes see it when i couldnt

Thanks to everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.