Monday, July 9, 2018

Python Scripting -BMI(Body Mass Index)

#!/usr/bin/env python3.7 2 3 #BMI = (weight in kg / height in meters sqaured) 4 #Imperial version: BMI * 703 5 6 def gather_info(): 7 height = float (input("What is your height? (inches or meters)")) 8 weight = float(input("What is your weight? (pounds or kilograms)")) 9 system = input("Are your measurements in metric or imperial units?").low er().strip() 10 return (height, weight, system) 11 12 def calculate_bmi(weight, height, system = 'metric'): 13 ''' 14 Return the Body Mass Index (BMI) for the 15 given weight, height and measurement system. 16 ''' 17 if system == 'metric': 18 bmi = (weight / (height ** 2)) 19 else: 20 bmi = 703 * (weight / (height **2)) 21 return bmi 22 23 while True: 24 height, weight , system = gather_info() 25 if system.startswith('i'): 26 bmi = calculate_bmi(weight, system=system,height= height) 27 print(f"Your BMI is {bmi}") 28 break 29 elif system.startswith('m'): 30 bmi = calculate_bmi(weight, height) 31 print(f"Your BMI is {bmi}") 32 break

No comments:

Post a Comment