Created
March 4, 2014 19:08
-
-
Save thoolihan/9353388 to your computer and use it in GitHub Desktop.
Python BMI Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def invalid(msg): | |
print msg | |
exit() | |
def getFloat(prompt): | |
print prompt + ":", | |
input = raw_input() | |
if len(input) > 0: | |
val = float(input) | |
else: | |
invalid("you entered nothing, nihilist") | |
return val | |
def getRating(bmi): | |
rating = "" | |
if bmi < 18.5: | |
rating = "underweight" | |
elif bmi < 25: | |
rating = "normal" | |
elif bmi < 30: | |
rating = "overweight" | |
else: | |
rating = "obese" | |
return rating | |
weight = getFloat("please enter weight (lbs)") | |
height = getFloat("please enter height (inches)") | |
desired_bmi = getFloat("please enter desired bmi") | |
current_bmi = weight / (height ** 2) * 703 | |
current_rating = getRating(current_bmi) | |
desired_weight = (desired_bmi * height ** 2) / 703 | |
desired_rating = getRating(desired_bmi) | |
print "you are %0.1f inches tall and weigh %0.1f lbs" \ | |
% (height, weight) | |
print "your bmi is currently %0.3f which is considered: %s" \ | |
% (current_bmi, current_rating.upper()) | |
print "your desired bmi is %0.3f which is considered: %s" \ | |
% (desired_bmi, desired_rating.upper()) | |
print "to do that, you will need to weigh %0.1f lbs" % desired_weight | |
print "which is a change of %0.1f lbs" % (desired_weight - weight) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment