How to Create a BMI Calculator in Python

In this tutorial, we will program a "How to Create a BMI Calculator in Python". We will delve deep into how to create this BMI calculator. The main goal here is to understand the structure of the program for calculating a person's BMI. I will provide a sample program to demonstrate the actual coding of this tutorial.

This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming this bmi calculator. I will do my best to provide you with the easiest method of creating this program, called "BMI calculator". So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display the BMI Calculator. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.     ret=False
  3.  
  4.     print("\n\n================= BMI Calculator  =================\n\n")
  5.  
  6.  
  7.     height = float(input("Enter your height in cm: "))
  8.     weight = float(input("Enter your weight in kg: "))
  9.  
  10.     BMI = weight / (height/100)**2
  11.  
  12.     print(f"You BMI is {BMI}")
  13.  
  14.     if BMI <= 18.4:
  15.         print("You are underweight.")
  16.     elif BMI <= 24.9:
  17.         print("You are healthy.")
  18.     elif BMI <= 29.9:
  19.         print("You are over weight.")
  20.     elif BMI <= 34.9:
  21.         print("You are severely over weight.")
  22.     elif BMI <= 39.9:
  23.         print("You are obese.")
  24.     else:
  25.         print("You are severely obese.")
  26.  
  27.     user_input=input("\n\nTry again? (Yes/No): ")
  28.     print("\n\n")
  29.     if user_input.lower()=='no':
  30.         print("Exit program.")
  31.         break
  32.  
  33.     elif user_input.lower()=='yes':
  34.         ret=True
  35.  
  36.     else:
  37.         print("Wrong input.")
  38.         ret=True
  39.  
  40.  
  41.     if ret:
  42.         continue

In the provided code, the first step is to use a while loop to enclose all the code to enable the restarting of the program. Next, we set variables to gather the user basic information such as height and weight. Lastly we will add some formula that will calculate the person BMI.

Output:

The How to Create a BMI Calculator in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a BMI Calculator in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment