Python BMI Calculator Project - Solutions and Explanations
BMI Calculator:
Develop a Body Mass Index calculator based on user input.
Input values:
User provides weight (in kilograms) and height (in meters).
Output value:
Calculated Body Mass Index (BMI) value and the corresponding BMI category.
Example:
Input values: Enter weight (in kilograms): 70 Enter height (in meters): 1.75 Output value: BMI: 22.9 Category: Normal Weight
Here are two different solutions for a Body Mass Index (BMI) calculator in Python. The BMI is calculated based on user input for weight (in kilograms) and height (in meters), and the corresponding BMI category is determined.
Solution 1: Basic Approach using Functions
Code:
# Solution 1: Basic Approach Using Functions
def calculate_bmi(weight, height):
"""Calculate the BMI based on weight and height."""
# Calculate BMI using the formula: BMI = weight / (height * height)
bmi = weight / (height ** 2)
return bmi
def determine_bmi_category(bmi):
"""Determine the BMI category based on BMI value."""
if bmi < 18.5:
return "Underweight"
elif 18.5 <= bmi < 24.9:
return "Normal Weight"
elif 25 <= bmi < 29.9:
return "Overweight"
else:
return "Obesity"
def main():
"""Main function to get user input, calculate BMI, and display the result."""
# Get user input for weight and height
weight = float(input("Enter weight (in kilograms): "))
height = float(input("Enter height (in meters): "))
# Calculate BMI
bmi = calculate_bmi(weight, height)
# Determine the BMI category
category = determine_bmi_category(bmi)
# Display the results
print(f"BMI: {bmi:.1f}")
print(f"Category: {category}")
# Start the BMI Calculator
main()
Output:
Enter weight (in kilograms): 70 Enter height (in meters): 1.76 BMI: 22.6 Category: Normal Weight
Enter weight (in kilograms): 83 Enter height (in meters): 1.74 BMI: 27.4 Category: Overweight
Explanation:
- Defines two functions: 'calculate_bmi()' to compute the BMI and 'determine_bmi_category()' to categorize the BMI value.
- The 'main()' function handles user input, calls the other functions to compute and categorize the BMI, and prints the result.
- This solution is simple and straightforward but less organized for expansion or reuse in larger projects.
Solution 2: Using a Class-Based approach for Organization and Reusability
Code:
# Solution 2: Using a Class-Based Approach for Organization and Reusability
class BMICalculator:
"""Class to handle BMI calculation and categorization."""
def __init__(self, weight, height):
"""Initialize with user-provided weight and height."""
self.weight = weight
self.height = height
def calculate_bmi(self):
"""Calculate the BMI based on weight and height."""
# Calculate BMI using the formula: BMI = weight / (height * height)
return self.weight / (self.height ** 2)
def determine_bmi_category(self, bmi):
"""Determine the BMI category based on BMI value."""
if bmi < 18.5:
return "Underweight"
elif 18.5 <= bmi < 24.9:
return "Normal Weight"
elif 25 <= bmi < 29.9:
return "Overweight"
else:
return "Obesity"
def display_results(self):
"""Calculate BMI, determine the category, and display the results."""
# Calculate BMI
bmi = self.calculate_bmi()
# Determine the BMI category
category = self.determine_bmi_category(bmi)
# Display the results
print(f"BMI: {bmi:.1f}")
print(f"Category: {category}")
# Get user input for weight and height
weight = float(input("Enter weight (in kilograms): "))
height = float(input("Enter height (in meters): "))
# Create an instance of the BMICalculator class
bmi_calculator = BMICalculator(weight, height)
# Display the results
bmi_calculator.display_results()
Output:
Enter weight (in kilograms): 72 Enter height (in meters): 1.76 BMI: 23.2 Category: Normal Weight
Enter weight (in kilograms): 85 Enter height (in meters): 2 BMI: 21.2 Category: Normal Weight
Explanation:
- Defines a 'BMICalculator' class to encapsulate all the functionality related to BMI calculation and categorization.
- The '__init__' method initializes the class with user-provided weight and height.
- The 'calculate_bmi()' method computes the BMI, while ‘determine_bmi_category()’ determines the category based on the computed BMI.
- The 'display_results()' method handles the complete process of calculating and displaying the BMI and its category.
- This approach follows Object-Oriented Programming (OOP) principles, making the code more modular, reusable, and easier to extend.
Note:
Both solutions effectively calculate and categorize BMI based on user input, with Solution 1 providing a basic function-based approach and Solution 2 offering a more structured, class-based design for better organization and scalability.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/projects/python/python-bmi-calculator-project.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics