Calculating Calories Burnt During Workout Using Python

Do you wonder how many calories you’ve burnt after a heavy workout? Not only health enthusiasts or body-builders, in today’s world everyone should exercise and pay attention to their physical health. And with the increasing availability of smart watches and smartphones, everyone can calculate the calories burnt during lifting, running or biking.

In this article, we will look at the basic logic behind the working of these devices that track your burned calories using your physical features such as height, weight, and physique and some temporary parameters such as your heart rate and body temperature.

This is a fun project that you can try even if you’re a beginner as the bulk of this program depends on user input. In order to understand how calories are calculated in your smart watch or in your smart phone, we will take a look at the formula for calculating calories burnt.

What are calories?

Calories are units of energy that a food or a drink contains. It is also the unit of measuring energy in the body. Calories are also used to measure the energy released when food breaks down in the body. We need enough calories to stay warm, move around, to think, to grow and do daily activities. If you consume more than the required calorie, your body stores it as fat.

Hence to stay healthy and burn the unnecessary calories in your body, you need to implement a healthy lifestyle in today’s world where packaged foods are the go-to containing extra calories.

Calculating calories burned during workout

We can easily calculate the number of calories burnt during workout using the parameters given below:

• Age
• Weight
• Heart Rate
• And the time or duration of workout.
• Also, the gender of the person, whether they are male or female.

The formula for calculating calories burned during workout for men is : Calories = [(Age x 0.2017) + (Weight x 0.09036) + (Heart Rate x 0.6309) — 55.0969 ] x Time / 4.184

The formula for calculating calories burned during workout for women is :  Calories = [ (Age x 0.074) — (Weight x 0.05741) + (Heart Rate x 0.4472) — 20.4022 ] x Time / 4.184

Check out: Hawaiian Pronouncer in Python

Implementing the python code

For this particular program, you won’t need any special package installation. Just install python and any editor that you want and you are all set for this beginner level project. Make sure you have installed the latest version of python as all of the code shown below is compatible only with python version >= 3.0

We will first define a function using the keyword `def function_name(gender,age,weight,heart_rate,time)` where the arguments inside the braces will be from the user. A function call will be made in the driver code and all the calculations will be done inside the function based on the user input.

Let’s get started!

```# code to calculate calories burnt during workout
# no special packages required
# creating function
def countcals(gender, age, weight, heart_rate, time):
# determining the gender
if gender == "male":
# implementing calculation for male
calories = (
((age * 0.2017) + (weight * 0.09036) + (heart_rate * 0.6309) - 55.0969)
* time
) / 4.184
elif gender == "female":
# implementing calculation for female
calories = (
((age * 0.074) + (weight * 0.05741) + (heart_rate * 0.4472) - 20.4022)
* time
) / 4.184
else:
# error raised
calories = "Cannot be calculated, wrong input!"
# function will return calories burnt based on the formulae
return calories

# driver code
# getting user input
gender = input("Enter gender,(male/female)= ")
age = int(input("Enter age in years only= "))
weight = int(input("Enter weight in kilograms only= "))
heart_rate = int(input("Enter heart rate= "))
time = int(input("Enter duration of workout in hours only= "))
# function call
burnedcals = countcals(gender, age, weight, heart_rate, time)
# display result
print("the calories you burnt were= ", burnedcals)

```

The output of the above code would be something as follows:

```Enter gender,(male/female)= female
Enter age in years only= 21
Enter weight in kilograms only= 50
Enter heart rate= 175
Enter duration of workout in hours only= 2
the calories you burnt were=  29.771653919694064
```

Similar: Creating a Golf Scores Program in Python

Summary

In this article, we have gone through the basic definition of what calories are and how you can calculate the number of burnt calories while you are work out. In this day and age, where technology has improved by leaps and bounds, it is very easy to look at our smart watches or our smartphones to determine how much exercise we’ve done. But in this article, we have explained how you can implement the basic mechanism of counting burnt calories while working out using a simple Python script and some formulae based on user input. Since most of the time, it depends on your body the amount of exercise that you need, user input plays a huge role in this tutorial. We hope you’ve had fun writing this program !