jchud13
jchud13

Reputation: 55

I don't understand why I am being told I have a syntax error

def BMI_calculator(inches, weight):
    """ This function takes a persons height in feet and inches and their
weight in pounds and calculates their BMI"""

# Now we can just convert and calculate BMI

    metric_height= inches* .025
    metric_weight= weight* .45
    BMI = int(metric_weight/(metric_height)**2)
    print BMI
    return BMI

def BMI_user_calculator():
    """ This function will ask the user their body information and calulate
the BMI with that info"""
# First we need to gather information from the user to put into the BMI calculator
    user_weight = int(raw_input('Enter your weight in pounds: '))
    user_height = int(raw_input('Enter your height in inches: '))

    print "Youre BMI is", 

    BMI_calculator(user_height, user_weight)

    If BMI < 18.5:
        print "You're BMI is below the healthy range for your age"
    elif BMI > 24.9:
        print "You're BMI is above the healthy range for your age"
    else:
        print "You are in the healthy BMI range!"

Here is the code I have so far, but when run I get a syntax error within my if statement saying BMI is not defined. BMI was returned from the first function so I really don't understand what is happening

Upvotes: 0

Views: 86

Answers (1)

afagarap
afagarap

Reputation: 649

You don't have BMI declared in your BMI_user_calculator() that's why it says BMI is not defined. You should declare BMI first before using it for comparison in if-elif statement.

In addition, your If should be if. Python is case-sensitive.

Your code should then read something like this:

def BMI_calculator(inches, weight):
    metric_height= inches* .025
    metric_weight= weight* .45
    BMI = int(metric_weight/(metric_height)**2)
    # No need to print BMI here anymore since you're returning it anyway
    # print BMI
    return BMI

def BMI_user_calculator():
    user_weight = int(raw_input('Enter your weight in pounds: '))
    user_height = int(raw_input('Enter your height in inches: '))

    BMI = BMI_calculator(user_height, user_weight)

    print "Your BMI is", BMI

    if BMI < 18.5:
        print "Your BMI is below the healthy range for your age"
    elif BMI > 24.9:
        print "Your BMI is above the healthy range for your age"
    else:
        print "You are in the healthy BMI range!"

if __name__ == '__main__':
    BMI_user_calculator()

Upvotes: 1

Related Questions