"""Program to calculate BMI that demonstrates many commonly used utilities """ # function for caclulating BMI (weight in kilos / height in meters squared) def BMI_calc (height,weight): height_meters = height * 0.0254 # converts inches to meters height_m_sqr = height_meters ** 2 # squares height in meters weight_kilos = weight * 0.453592 # converts pounds to kilos BMI = weight_kilos / height_m_sqr return BMI # returns the final calculated BMI # routine for obtaining height in either inches or feet and inches noProb = False while noProb == False: inp_height = raw_input("How tall are you in 'inches' or in 'feet, inches'? ") try: if ',' in inp_height: feet, inches = inp_height.split(',') height = float(feet) * 12 + float(inches) else: height = float(inp_height) noProb = True except: print 'Enter height in inches as a number.' # routine for obtaining weight in pounds noProb = False while noProb == False: inp_weight = raw_input('What is your weight in pounds? ') try: weight = float(inp_weight) noProb = True except: print 'Enter weight as a number.' print BMI_calc(height, weight)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question