Python BMI Program Exercise


#1

Body Mass Index

Design a modular program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:

#When i run this code, my introduction does not show up first.
#“Please enter your height in inches.” comes up first")
#How do i fix this?
#I need at least two functions including the def #main():

def main():
    bmi_intro()
    

#define variables
get_height = 0.0
get_weight = 0.0
body_mass_index = 0.0


#We will start with the introduction to our program
def bmi_intro():
    print("Welcome to my BMI calculator!")
    print("If you can tell me your weight and height")
    print("I can tell you your Body Mass Index")
    print("Let's Go!\n")

#From this point I will ask the user for his/her information.

get_height = float(input("Please enter your height in inches. "))
get_weight = float(input("Please enter your weight in pounds. "))
#user will enter there information above and we will then calcualte.
body_mass_index = (get_weight * 703) / (get_height ** 2)




if body_mass_index < 18.5:
    print("A person with a BMI of " + str(body_mass_index ) + " is underwieght ")
elif body_mass_index < 24.9:
    print("A person with a BMI of " + str(body_mass_index ) + " is normal weight ")
else:
    print("A person with a BMI of " + str(body_mass_index ) + " is overweight ")

main()

#2

Code outside of the functions seems to be the problem.

def bmi_intro():
    print("Welcome to my BMI calculator!")
    print("If you can tell me your weight and height")
    print("I can tell you your Body Mass Index")
    print("Let's Go!\n")

def main():
    bmi_intro()    
    get_height = 0.0
    get_weight = 0.0
    body_mass_index = 0.0
    get_height = float(input("Please enter your height in inches. "))
    get_weight = float(input("Please enter your weight in pounds. "))
    body_mass_index = (get_weight * 703) / (get_height ** 2)
    if body_mass_index < 18.5:
        print("A person with a BMI of " + str(body_mass_index ) + " is underwieght ")
    elif body_mass_index < 24.9:
        print("A person with a BMI of " + str(body_mass_index ) + " is normal weight ")
    else:
        print("A person with a BMI of " + str(body_mass_index ) + " is overweight ")

main()
Welcome to my BMI calculator!
If you can tell me your weight and height
I can tell you your Body Mass Index
Let's Go!

Please enter your height in inches. 75
Please enter your weight in pounds. 200
A person with a BMI of 24.995555555555555 is overweight 

Funny that it says I’m overweight. My own doctor insists I keep my weight right around where it is, and I’m quite slim. At any length, the program works now.

refactored
def bmi_intro():
    print("Welcome to my BMI calculator!")
    print("If you can tell me your weight and height")
    print("I can tell you your Body Mass Index")
    print("Let's Go!\n")

def main():
    bmi_intro()    

    height = float(input("Please enter your height in inches. "))
    weight = float(input("Please enter your weight in pounds. "))
    
    bmi = (weight * 703) / (height ** 2)
    weight_range = 'underweight' if bmi < 18.5 else 'normal weight' if bmi < 24.9 else 'overweight'

    print ("A person with a BMI of {} is {}.".format(bmi, weight_range))

main()

#3

Thank you so much for this. It really helped out a lot!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.