How to Write Python Code That Calculate Your BMI

by 0xrakan in Circuits > Computers

716 Views, 2 Favorites, 0 Comments

How to Write Python Code That Calculate Your BMI

BMI.jpg

Learning how to achieve a task like calculating your BMI using just a few codes can help you acquire the skill and the knowledge for one of the most required skills in the 21st century, coding. If you have ever wondered how programmers accomplish tasks using only codes, you will learn how to calculate your BMI by writing python code from scratch. By following this task step by step, you will learn the basics of programming in the python language, how to calculate your BMI, and how each step is accomplished. Accomplishing this task by programming is truly effective because it will help you understand how a simple program is working and how a few lines of codes work together to deliver a final program that does something we want, like calculating the BMI.

Image retrieved from ausmed

Setting Up the Code Editor

Download.PNG
steps4.PNG

To write a code using whatever programming language, you need an editor that helps you write the code lines that will accomplish a task, in our case, calculating the BMI. We will use PyCharm, one of the best editors for the python language.

This is an overview of setting up a code editor to use for writing the code for the BMI calculator:

  1. Go to this website and click download the community version(depending on which operating system you use)
  2. the application wizard will start to download
  3. Click on the wizard to start setting up your code editor
  4. A pop-up window will appear; click yes
  5. On the welcome page, click next.
  6. If you have an older version of this editor, you can uninstall it and install the newer version.
  7. Multiple windows will appear; click next until you see a window with an "install" button.
  8. You will see a green bar indicating that the download process is started.
  9. Click next after the green bar is completed.
  10. A final window will appear that indicates that you installed the app successfully.
  11. Check the small box to launch the app and click finish.

Following is a brief detail about each step.

On the Welcome Page, Click Next.

steps1.PNG

After the installation wizard has been installed, you will need to click it, and then a pop-up window will appear that asks for your permission to make this application make some changes to your device; click yes.

This window will appear, which is the beginning of the editor setting up process; click next.

Note: it is normal for every application to ask you for permission to make some changes for security reasons.

If You Have an Older Version of This Editor, You Can Uninstall It and Install the Newer Version.

steps2 - Copy.PNG

If you have ever attended a programming workshop using python, you'd probably installed an older version of this application. Therefore, a new window will appear after you click next that asks you to uninstall any older versions you have for the same application and install the new one; check both boxes.

Multiple Windows Will Appear; Click Next Until You See a Window With an "install" Button.

steps5.PNG

After you uninstalled the older versions, multiple windows will appear to ask you for specific configuration settings; for now, click next until you see a window with an install button.

Click install to start the installation process.

You Will See a Green Bar Indicating That the Download Process Is Started.

steps6.PNG

After you click install, you will see a green bar that indicates that the installation process has begun. Click next after the green bar is completed.

A Final Window Will Appear That Indicates That You Installed the App Successfully.

steps7.PNG

Check the small box to launch the app and click finish. After doing that, the app will launch, and you will be able to access it and set up the file that you'll be using for writing your code.

Setting Up the Python File to Write the Code

steps9.png
steps8.PNG
  1. From the file tab, click new.
  2. Click "Python File."
  3. Name the file BMI or whatever you like; a small window will appear.

After Creating Your Python File, You Are Ready to Write Your Code.

empty.PNG

If you see this window that looks like the picture, you can assume that you have successfully created a python file and are ready to learn some basic code syntax and write your first basic program.

Note: the file will be empty at first.

Some Basics Python Syntax

steps11.png

def ==> is for defining a function that will hold the code lines; it will have the following syntax:

def "name of the function" ():

input==> is for taking information from the user

float==> is to specify what kind of numbers is accepted

Print ==> is to print whatever information we pass to it.

if==> for stating a condition

elif ==> is for stating a condition after the first one.

else==> is for the last condition if none of the above is satisfied.

A variable is a placeholder to hold the information. so if we want to create one to hold the height information, we can write like this: height = float(input("Please enter your height in cm : "))

To make the function executable, write the function's name at the end of the file.

for example, the function name is BMI; I will write the end of the file: BMI()

Note: for more details on each line and its functions, please click on the picture to learn more.

Write the Same Code Into Your Editor

steps11.png

After you learn about the python syntax and what each line means and does, you can copy the following lines and paste them into your code editor or write them from scratch as you wish.

def BMICal():

height = float(input("Please enter your height in cm: ")) // this for taking height info. from the user

weight = float(input("Please enter your weight in Kg: ")) // this for taking weight info. from the user

BMI = weight / (height / 100) ** 2 // the BMI formula "BMI = kg/m2" , we divide the height to convert it to meters.

print(f"You BMI is {BMI}") // we used "f" which means format, to pass the BMI variable.

if BMI <= 18.4: print("You are underweight.")

elif BMI <= 24.9: print("You are healthy.")

elif BMI <= 29.9: print("You are over weight.")

elif BMI <= 34.9: print("You are severely over weight.")

elif BMI <= 39.9: print("You are obese.")

else: print("You are severely obese.")

BMICal()

Right Click on the Code to Run Your Code

steps10.png

After you have written your code and made sure that it is logically valid and its syntax is correct, you can launch your simple program to calculate your BMI.

Click run "the name you choose for your function."

Enter the Information to Learn About Your BMI

steps12.png

Please note that expended notes and explanations are included in the picture to tell you which line of your code is executed in the resulting image.

Note: the result will vary depending on the information you entered.

Conclusion

After you accomplish your first task using the python programming code, you have a more robust foundation for writing programs in python. This task is meant to teach you the basic syntax of the python language in a simple way and learn how to set up an editor that will be your friend in your learning journey. By accomplishing this task successfully, you learned how to write a simple program and some basic skills like thinking logically and analyzing the code. These skills can help you in the future as you are looking for a job that requires these skills. Remember that each extensive program like Twitter or Amazon is started with a few lines of code like the ones you learned, so be proud that you learned a valuable skill that you can be used in the future and make some money from it.