5
\$\begingroup\$

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!')
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Connor Bornak Apr 19 at 19:26
  • \$\begingroup\$ 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. \$\endgroup\$ – Montana Burr May 15 at 19:16
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Your code is neat and understandable, but you can make it neater.

  • Always leave space between operator and operands.
  • snake_case names are better than camelCase names.
  • Don't leave spaces between functions like print (). Always use print().
  • Comments are needed only in obscure parts of the code to explain its workings.
  • Why input1? Use an easily understandable name such as unit_type.
  • Use elif only if necessary. That way, you can avoid repetition.

Python allows you to make multiple comparisions in an if statement.
BMI >= 19 and BMI <= 24 can be rewritten as 19 <= BMI <= 24

Use python's exponent operator **.
You could write heightIN ** 2 instead of heightIN * heightIN

Learn to use formatting. Calculate the result first ('obese', 'healthy weight', etc.) and use it in a string.

An improvised piece of code:

if unit_type == 'imperial':
    height_in = float(input('Input your height in Inches (in): '))
    weight_lb = float(input('Input your weight in Pounds (lbs): ' ))
    BMI = round(((weight_lb / (height_in ** 2)) * 703), 2) 

    result = ''

    if 19 > BMI: result = 'underweight'
    if 39 < BMI: result = 'extremely obese!'
    if 19 <= BMI <= 24: result = 'healthy weight'
    if 25 <= BMI <= 29: result = 'overweight'
    if 30 <= BMI <= 39: result = 'obese'

    print('Your BMI is {0} so, you are {1}!'.format(BMI, result))

Hope this helps!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.