Welcome, guest | Sign In | My Account | Store | Cart

This code helps you figure out your BMI! Can perform three calculations. Also has a built in quit function. NEW! now can figure up to 10 calculations! I also have a metric version here: http://code.activestate.com/recipes/577907-bmi-calculator-metric/

Python, 54 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54``` ```#!/usr/bin/env python ##Author: Alex Cruz # If user types in q, program terminates # If user types in other than q, program # continues and calcualtes bmi def main(): quit = False count = 0 answer = raw_input('\nEnter "q" to quit: ') if answer == 'q': quit = True # Caclulates bmi 3 times prompting user to type in 'q' # or another letter to continue with bmi calculation while (not quit and count < 10): print '-------------------------' print 'This is to ' print '-------------------------' weight = float(input('Enter weight in pounds: ')) height = float(input('Enter height in inches: ')) # Weight cannot be less than 0 or greater than 500 if weight <= 0 or weight > 500: print 'Weight cannot be less than 0 or greater than 500' continue # Height cannot be less than or equal to 0 inches elif height <= 0: print 'Height cannot be less than 0' continue else: bmi = (weight / (height * height)) * 703.0 print 'Your BMI is %.2f' % bmi if bmi <= 18.5: print 'Your weight status is Underweight' elif bmi >= 18.5 and bmi <= 24.9: print 'Your weight status is Normal weight' elif bmi >= 25 and bmi <= 29.9: print 'Your weight status is Overweight' elif bmi >= 30: print 'Your weight status is Obese' # increments bmi calculation by 1 with a total of 3 bmi calculations count = count + 1 if count < 10: answer = raw_input('\nEnter "q" to quit: ') if answer == 'q': quit = True main() raw_input('\n to exit') ```

Tell me if you have any improvements you would like to see.

Alexander Thomas Cruz (author) 12 years, 6 months ago

You can change two numbers to make it do more calculations.

Michal Niklas 12 years, 6 months ago

I think you don't need comment code where the same information is displayed in message: "Weight cannot be less than 0 or greater than 500". This way if you change requirements you will have to remember to change it in 3 places: comment, conditional and message. I prefer making constants like MAX_WEIGTH and code like:

``````# check if weight is reasonable
if weight <= MIN_WEIGHT or weight > MAX_WEIGHT:
print('Weight cannot be less than %d or greater than %d' % (MIN_WEIGHT, MAX_WEIGHT))
``````

And why only imperial units? I don't know if you can check locale to see if metric system is more useful. Also you can use command line arguments; maybe something like:

``````bmi.py -metic -weight=75 -height=178
``````
Alexander Thomas Cruz (author) 12 years, 6 months ago

I considered the metric problem and forked this recipe.

 Created by Alexander Thomas Cruz on Sat, 8 Oct 2011 (MIT)

### Required Modules

• (none specified)