# Bmi calculator in python too many if statements how to reduce that

When writing a BMI calculator in Python, it is common to use multiple if statements to handle different conditions. However, this can lead to code duplication and make the code harder to read and maintain. In this article, we will explore three different ways to solve this problem and reduce the number of if statements.

## Option 1: Using a Dictionary

One way to reduce the number of if statements is by using a dictionary to map the BMI ranges to their corresponding categories. Here’s an example:

``````
def calculate_bmi(height, weight):
bmi = weight / (height ** 2)

categories = {
(0, 18.5): "Underweight",
(18.5, 24.9): "Normal weight",
(24.9, 29.9): "Overweight",
(29.9, float('inf')): "Obese"
}

for range_, category in categories.items():
if range_[0] <= bmi < range_[1]:
return category

return "Invalid BMI range"
``````

In this approach, we define a dictionary where the keys are tuples representing the BMI ranges, and the values are the corresponding categories. We then iterate over the dictionary items and check if the calculated BMI falls within any of the ranges. If a match is found, we return the corresponding category. Otherwise, we return an "Invalid BMI range" message.

## Option 2: Using a List of Tuples

Another approach is to use a list of tuples to store the BMI ranges and categories. Here's an example:

``````
def calculate_bmi(height, weight):
bmi = weight / (height ** 2)

categories = [
(0, 18.5, "Underweight"),
(18.5, 24.9, "Normal weight"),
(24.9, 29.9, "Overweight"),
(29.9, float('inf'), "Obese")
]

for range_ in categories:
if range_[0] <= bmi < range_[1]:
return range_[2]

return "Invalid BMI range"
``````

In this approach, each tuple in the list represents a BMI range and its corresponding category. We iterate over the list and check if the calculated BMI falls within any of the ranges. If a match is found, we return the corresponding category. Otherwise, we return an "Invalid BMI range" message.

## Option 3: Using a Class

A more object-oriented approach is to define a class that encapsulates the BMI ranges and categories. Here's an example:

``````
class BMICalculator:
def __init__(self):
self.categories = [
(0, 18.5, "Underweight"),
(18.5, 24.9, "Normal weight"),
(24.9, 29.9, "Overweight"),
(29.9, float('inf'), "Obese")
]

def calculate_bmi(self, height, weight):
bmi = weight / (height ** 2)

for range_ in self.categories:
if range_[0] <= bmi < range_[1]:
return range_[2]

return "Invalid BMI range"

# Usage example
calculator = BMICalculator()
category = calculator.calculate_bmi(1.75, 70)
print(category)
``````

In this approach, we define a BMICalculator class with a list of tuples representing the BMI ranges and categories. The calculate_bmi method calculates the BMI and checks if it falls within any of the ranges. If a match is found, it returns the corresponding category. Otherwise, it returns an "Invalid BMI range" message.

After exploring these three options, it is clear that using a class provides the most flexibility and maintainability. It allows for easy extension and modification of the BMI ranges and categories. Additionally, it promotes code reusability by encapsulating the logic within a class. Therefore, using a class is the recommended option for solving the problem of reducing if statements in a Python BMI calculator.

Rate this post

### 5 Responses

1. Abdullah says:

Option 2 is the way to go! Tuples add that extra spice to the code. 🌶️

2. Case Ruiz says:

Option 4: Using a Magic 8 Ball! Shake it and get your BMI, no if statements needed! 🎱😂

3. Jesus Garrison says:

Option 2 with the List of Tuples sounds like a cool way to simplify the BMI calculator code. 🤔📝

4. Marcelo Keller says:

Option 2: Using a List of Tuples seems like the most straightforward and efficient solution. #simplifycoding

5. Alexa says:

Option 4: Using a magic wand and casting a spell to simplify the code! ✨🧙‍♀️