Coding a BMI Calculator in Python


Coding a BMI Calculator in Python

         My Java Script BMI Calculator post, which was written a little over three years ago still generates a decent amount of traffic. We’ve also previously covered writing a VBA code for this calculation. Today we’ll try to refresh our programming skills and take a stab at building a Python version of this code. There are some great reasons why Python is overtaking R in becoming the leading programming language for data science projects. Before we delve into any data analysis type of exercise, I thought it would be helpful to get orientated to this language via easy to follow calculation: BMI = (Weight/(Height^2)) * 703.06957964 , where Weight is measured in pounds , while Height is in inches . Before we write the first line of code, one thing to keep in mind is that while Python is a fairly easy programming language to understand and follow along; it’s rather peculiar as far as indentation is concerned. Please exercise a due care to ensure that your code compiles successfully by eliminating all of the extra blank spaces in your code and following proper indentation rules.
Continue reading

Creating user-defined Excel functions (UDFs) – Temperature Conversion and BMI Calculation functions from scratch.

Excel UDF, custom function to convert temperatures and BMI calculator


         Our last two posts focused on using, or working with somebody else’s VBA code. This article will focus on creating UDF functions from scratch. We will start with a simple temperature conversion from Celsius to Fahreheit, and vice versa. We will then calculate metric Body Mass Index (BMI), graduating to an imperial BMI calculation with 3 variables.

         Since we’ve already worked with VBA code, we know some basic syntax rules for our future functions:

                  1. Start out with Option Explicit statement.
         Note : Option Explicit statement forces explicit declaration of our variables, and gives us an error message if we have a typo instead of performing incorrect calculations.
                  2. Define our function name and its parameter(s)
                   Function FunctionName(Parameter DataType) As DataType.
         Note : VBA makes definining data types optional, but it is a good habit to have.
                  3. Perform our calculations: Your code goes here.
                  4. Exit function and return it’s value: End Function.

Continue reading