Skip to content

Instantly share code, notes, and snippets.

@HopperMCS
Last active September 19, 2016 22:51
Show Gist options
  • Save HopperMCS/66b48d8214338d00fb3725e663ec7c7e to your computer and use it in GitHub Desktop.
Save HopperMCS/66b48d8214338d00fb3725e663ec7c7e to your computer and use it in GitHub Desktop.
Gage's Python2 BMI Calculator
"""
This is a BMI calculator written in Python by me (MGage Morgan). The design is minimal atm, but will improve as I get further.
The BMI formula to use: weight / height^2 x 703
We use a float() wrapped around a raw_input() for 'weight' and 'height'. We do this to prevent Python from thinking we're trying to
use strings instead of numbers when actually we are not.
"""
weight = float(raw_input('Enter your weight:\n '))
height = float(raw_input('Enter your height:\n '))
"""
Square splits off a part of the formula to do first (think: PEMDAS). The Order of Operations from math says to do exponents before
multiplying/dividng. So the 'square' variable holds the value of 'height' being squared so we can conveniently use it later.
"""
square = height ** 2
"""
'divvy' is the second piece of the problem. This variable holds the 'weight' divided by the pre-calculated 'height squared' to make
the problem into just two pieces now.
"""
divvy = weight / square
"""
The 'finale' brings it all together. We take the first half ('divvy') and multiply it by 703, as per the CDC's Adult BMI formula.
"""
finale = divvy * 703
"""
This is more for readability than anything. We round off the number given by 'finale' to the nearest hundredth. This way, it also
matches the response given on CDC's website.
"""
rounded_finale = round(finale, 2)
"""
Vóila!! The number can now be printed to the terminal/console as the result.
"""
print rounded_finale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment