Coding a simple Python BMI calculator using functions
Tagged: programming, python
- AuthorPosts
- May 29, 2020 at 4:33 PM #3379
Santhosh Kumar D
Keymaster@santhoshIn this tutorial, I am going to use Python 3.8.2 to code a simple Python BMI calculator. BMI is a way to assess whether your weight is in the healthy range. It is calculated as the ratio of weight in kilograms divided by height in metres squared.
Body Mass Index (BMI) = Weight (kg) / Height (m2)
A simple Python BMI calculator using functions
Step 1: In your text editor, create a new file and save it with the extension
.py
. For this tutorial, let’s name itbmi.py
.Step 2: Let’s add the code to receive weight and height inputs from the user. We are going to use
float()
andinput()
Python functions to achieve this.#Coding a simple Python BMI calculator # Receive input from the user weight=float(input("Enter your weight in kilograms:")) height=float(input("Enter your height in centimeters:"))
The
input()
function will simply allow the user to enter a text input. Next, thefloat()
function will convert the entered number into a floating point number.Step 3: Now let’s add the formula to calculate BMI.
# Calculate the BMI bmi=weight/((height/100)*(height/100))
We have added a variable called
bmi
. In Python, variable gives data to the computer for processing. The asterisk*
is used to perform multiplication and the forward slash/
is used to perform division. We have dividedheight
by100
to convert centimeters into meters and we have multipliedheight
withheight
in order to square it. And then we have dividedweight
byheight
and stored the value in the variablebmi
.Step 4: Let’s display the calculated BMI to the user.
# Display the result to the user print("Your Body Mass Index (BMI) is:",round(bmi,2))
The
print
command will print everything you have added inside the parentheses (brackets). Theround
command will round the resulting value to 2 decimal places.Step 5: Save the file as
bmi.py
. Your final code will look something like this.# Coding a simple Python BMI calculator # Receive input from the user weight=float(input("Enter your weight in kilograms:")) height=float(input("Enter your height in centimeters:")) # Calculate the BMI bmi=weight/((height/100)*(height/100)) # Display the result to the user print("Your Body Mass Index (BMI) is:",round(bmi,2))
Step 5: Now let’s run our Python script in the command-line interpreter.
Enter your weight in kilograms: 67 Enter your height in centimeters: 167 Your Body Mass Index (BMI) is: 24.02
More resources:
Built-in functions – Python 3 documentation - AuthorPosts
Advertisement
RECENT TIPS
- How to send email as an alias in Gmail?
- How to stop Google from tracking your location?
- Command to check the Ubuntu version
- How to add a percentage in Excel?
- How to convert currencies in Google Sheets?
- How to voice type on Google Docs?
- What is Django framework?
- How to turn Google Assistant off?
- How to check the Python version?
- How to create a virtual environment in Python?
- How to change the directory in the Command Prompt?
- How to add a shortcode in WordPress PHP template file?
- Useful websites to research stocks
- How to check Linux uptime?
- How to check Linux last reboot time?