# Simple BMI Calculator (Python 3)

As my first Python program I have written a simple BMI calculator. I want to know how i can improve this program. Would functions be useful? How can I improve the way that it is presented?

#Introduce BMI Calculator
print ('Welcome to BMI Calculator!')

#Ask if user would like to use Metric or Imperial units
input1 = input ('Would you like to input Metric units or Imperial units? ')

#If Imperial... If Metric... Calculate BMI...
if input1 == 'imperial':
heightIN = float(input('Input your height in Inches (in): '))
weightLB = float(input('Input your weight in Pounds (lbs):' ))
BMI = round(((weightLB/(heightIN*heightIN))*703), 2)

if BMI >= 19 and BMI <= 24:
print ('Your BMI is', BMI, 'so, you are healthy weight!')

elif BMI >= 25 and BMI <= 29:
print ('Your BMI is', BMI, 'so, you are overweight!')

elif BMI >= 30 and BMI <= 39:
print ('Your BMI is', BMI, 'so, you are obese!')

elif BMI > 39:
print ('Your BMI is', BMI, 'so, you are extremely obese!')

elif BMI < 19:
print ('Your BMI is', BMI, 'so, you are underweight!')

elif input1 == 'metric':
heightCM = float(input('Input your height in Centimeters (cm): '))
weightKG = float(input('Input your weight in kilograms (kg): '))
heightM = heightCM*0.01
BMI = round((weightKG/(heightM*heightM)), 2)

if BMI >= 19 and BMI <= 24:
print ('Your BMI is', BMI, 'so, you are healthy weight!')

elif BMI >= 25 and BMI <= 29:
print ('Your BMI is', BMI, 'so, you are overweight!')

elif BMI >= 30 and BMI <= 39:
print ('Your BMI is', BMI, 'so, you are obese!')

elif BMI > 39:
print ('Your BMI is', BMI, 'so, you are extremely obese!')

elif BMI < 19:
print ('Your BMI is', BMI, 'so, you are underweight!')

else:
print ('There was an error with your input. Please restart the program!')


It's probably best to use a function that uses SI units - you can then convert lbs to kgs and in to cm. Additionally, since the majority of the world either uses Imperial or metric, we can use a Boolean to indicate which one to use:

def BMI(height,weight,is_metric):
if (is_metric):
# Height in meters, weight in kg
return weight / height**2
else:
# Height in inches, weight in lbs
height = height * 0.0254
weight = weight * 0.453
return BMI(height,weight,True)

• Thanks for replying! I'm not really sure of how to implement this section of code into my program. I'm also not sure how the Boolean function knows what information it is checking with 'is_metric'. Is there any way that you could explain/show me how? – Connor Bornak Apr 19 '19 at 19:26
• What I would do is ask the user if they prefer metric or Imperial/US Customary units. Store the preference as a BOOLEAN that indicates whether metric was specified. Then, ask their height and weight and store them as floats. Call this function with these parameters and store the result in another variable that you can use to determine the user's health. – moonman239 May 15 '19 at 19:16

You aren't using loops or functions, so I assume you haven't learned them yet.

Your code is clean and readable, for the most part, so let me suggest just a few changes.

First, see PEP-8. It's the coding standard for Python (like it or not). Many of the issues I have with your code will be fixed by conforming to that standard:

• spaces between operands and operators

• snake_case names

Next, be aware that Python has an exponent operator, ** you can use for your computations.

Your comments are correct, but add no value. Delete any comment that simply explains in English what the next line obviously does in Python.

Finally, apply the DRY principle: don't repeat yourself! Those if...elif...else statements in each block are the same, since the index values are normalized. Move them to the bottom, after your first if else (Freedom vs. Metric units) and set the index to some bogus value in your error case.