Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Operators and Expressions

Implement a basic BMI calculator - Python Program

Example 1:

weight = float(input("Enter weight (kg): ")) height = float(input("Enter height (m): ")) bmi = weight / height ** 2 print(f"BMI = {bmi:.2f}")

Output

 
OUTPUT   :
Enter weight (kg): 60
Enter height (m): 1.75
BMI = 19.59

Example 2: Advanced Program

def calculate_bmi(weight_kg, height_m): """ Calculates the Body Mass Index (BMI). Args: weight_kg (float): The individual's weight in kilograms. height_m (float): The individual's height in meters. Returns: float: The calculated BMI. """ if height_m <= 0: raise ValueError("Height cannot be zero or negative.") bmi = weight_kg / (height_m ** 2) return bmi def interpret_bmi(bmi): """ Interprets the calculated BMI and returns a corresponding category. Args: bmi (float): The calculated BMI. Returns: str: The BMI category (e.g., "Underweight", "Healthy Weight"). """ if bmi < 18.5: return "Underweight" elif 18.5 <= bmi < 24.9: return "Healthy Weight" elif 24.9 <= bmi < 29.9: return "Overweight" else: return "Obesity" # Get user input try: weight = float(input("Enter your weight in kilograms (kg): ")) height = float(input("Enter your height in meters (m): ")) # Calculate BMI bmi_value = calculate_bmi(weight, height) # Interpret BMI bmi_category = interpret_bmi(bmi_value) # Display results print(f"\nYour BMI is: {bmi_value:.2f}") print(f"Based on your BMI, you are categorized as: {bmi_category}") except ValueError as e: print(f"Error: {e}. Please enter valid numerical values for weight and height.")

Output

 
OUTPUT 1 :
Enter your weight in kilograms (kg): 20
Enter your height in meters (m): 1.65

Your BMI is: 7.35
Based on your BMI, you are categorized as: Underweight 


OUTPUT 2 :
Enter your weight in kilograms (kg): 85
Enter your height in meters (m): 1.80

Your BMI is: 26.23
Based on your BMI, you are categorized as: Overweight

OUTPUT 3 :
Enter your weight in kilograms (kg): 75
Enter your height in meters (m): 1.80

Your BMI is: 23.15
Based on your BMI, you are categorized as: Healthy Weight