DEV Community

Cover image for Python scripting basics - BMI calculator
Soumyajyoti Biswas
Soumyajyoti Biswas

Posted on • Edited on

Python scripting basics - BMI calculator

Contents

Summary

In today's post I will walk you step by step on how to write a basic python script to calculate your body mass index(B.M.I). We will ask the user for some input, and based on it, calculate their B.M.I and return also tell the user, which category their B.M.I falls under. Body mass index is defined by the formula as below:

B.M.I = [your weight in (Kg) / (your height in (m))^2]
Kg - Kilogram
m - metre
Enter fullscreen mode Exit fullscreen mode

There are several defined B.M.I categories, such as:

Underweight = B.M.I below < 18.5.
Normal weight = B.M.I between 18.5-24.9.
Overweight = B.M.I between 25-29.9.
Obesity = B.M.I 30 or above 30.
Enter fullscreen mode Exit fullscreen mode

Code

Taking data from the user

  • We are going to use the input function, and store the data provided by the user in a variable.

Python Input Function

>>> user_name = input("Enter your name: ")
Enter your name: Soumyajyoti Biswas
>>> print(user_name)
Soumyajyoti Biswas
>>> 
Enter fullscreen mode Exit fullscreen mode
  • We are going to ask the user for the name of the user, their weight in Kg(s) and their height in cm(s)
user_name = input("Enter your name: ")
user_weight = float(input("Enter your weight in Kg(s): "))
user_height = float(input("Enter your height in cm(s): "))
Enter fullscreen mode Exit fullscreen mode

Calculating the B.M.I

  • Lets take the data that the user provided and put it through our B.M.I formula. Note that the user is providing their height in cm(s). So we will convert that to metre first. To convert cm to m, you divide it by 100.
>>> user_name = input("Enter your name: ")
Enter your name: Soumyajyoti Biswas
>>> user_weight = float(input("Enter your weight in Kg(s): "))
Enter your weight in Kg(s): 70
>>> user_height = float(input("Enter your height in cm(s): "))
Enter your height in cm(s): 165
>>> bmi = round(user_weight/((user_height/100) ** 2),1)
>>> print(bmi)
25.7
Enter fullscreen mode Exit fullscreen mode
  • In the above code I did two things:
    • [1] Convert the user input for weight and height from string to a float data type. The input function provides data output as type string. Hence we have to convert it to a float data type to perform numerical operation on it. See what error comes if you do not [Ref1]. You can see the various datatypes here.
    • [2] Round the result of B.M.I calculation to a single decimal. See how the round function works.
# Error displayed if you try to cast a string to a float. [Ref1]
>>> x = input()
12345
>>> type(x)
<class 'str'>
>>> x + x
'1234512345'
>>> type(x + x)
<class 'str'>
>>> x / 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'
Enter fullscreen mode Exit fullscreen mode

Getting the B.M.I category based on calculated B.M.I

  • Let us take the calculated B.M.I and try and place it in a category as defined above. We will use the If / elif / else to do that.
>>> if bmi < 18.5:
...   result = "Underweight"
... elif bmi >= 18.5 and bmi <= 24.9:
...   result = "Normal"
... elif bmi >= 25 and bmi <= 29.9:
...   result = "Overweight"
... else:
...   result = "Very overweight"
... 
>>> print(result)
Overweight
Enter fullscreen mode Exit fullscreen mode

Putting it all together

  • Let's put all the code together to build a script
user_name = input("Enter your name: ")
user_weight = float(input("Enter your weight in Kg(s): "))
user_height = float(input("Enter your height in cm(s): "))
bmi = round(user_weight/((user_height/100) ** 2),1)
if bmi < 18.5:
  result = "Underweight"
elif bmi >= 18.5 and bmi <= 24.9:
  result = "Normal"
elif bmi >= 25 and bmi <= 29.9:
  result = "Overweight"
else:
  result = "Very overweight"
print(f"Hello {user_name}. Your BMI is {bmi}. Your body mass index is {result}.")
Enter fullscreen mode Exit fullscreen mode
  • You can download the file from my GitHub page.

Top comments (0)