Python Calorie Tracker Tutorial

The aim of this lesson is practice some of the skills you have learnt so far, by making a calorie consumption tracker.

For this program, unlike previous programs, we are not going to do a lot of planning, because the program is going to evolve as we code it, so instead we are going to develop my program through a series of versions – coding the program until it gets to a certain level, then using ‘save as’ to move on to the next version.

Version 1 – Basic functionality

  • Ask the user to input their calorie intake for a period of 7 days at a time.
  • For each week display the total amount of calories consumed, the average and the days where most/least were consumed.

Version 2 – Input Validation & Advice

  • If the users inputs invalid data, get them to input it again.
  • If the user is eating too much or too little, give them a warning.

Version 3 – Data for 4 weeks

  • Display the calorie intake values for the last 4 weeks in total.

Version 4 –  Persistent File Storage 

  • The data should be stored in a text file for persistent storage.

Python Calorie Tracker Tutorial

Version 1 – Basic functionality

For this we  can start out and write a simple program the performs all the required functions for objective 1, and it could look a bit like this:

Version 1a (inefficient version)

This program works, but it is really quite inefficient and so it needs to be changed! remember that for the programming projects at GCSE level, in order to get full marks your program needs to be efficient!

Version 1b (efficient version)

Here we have swapped the 7 inputs for 1 input and put all the values in to a list. This will help us later on when we come to display the values for each day.

Version 2 – Input validation and advice

Version one of the program is already looking good, but it at the moment it is not very robust. If you enter anything other than a number in to the program it breaks.

If the user enters a ridiculous number ( such as -50 or 200000), it just happily accepts the number. To fix this, we need to use a function. This is will allow us to improve our program with the minimum amount of fuss and maximum benefit.

The getCalories() function

Edited code in the main file.

Final code version 2

Version 3 – Display four weeks of values

Now we have got our program working for 1 week, we need to get it working for all 4 weeks. To keep our program nice and simple, we are going to ask the user which week’s values they want to enter, and then add that week’s values. All our data is going to be stored in a 2 dimensional list ( a list within a list) and then it is going to be displayed on the main screen.

Functions as screens (lines 16 & 38)

Now that our program is going to be using more than one screen, it is a good idea to move our code in to functions for each screen. You can see this in action in the finished code below where our code is now split into 3 functions

  • main() – This is the screen that displays the values stored in the whole month.
  • getAWeek() – This is the screen that get’s the calorie values for a week.
  • getCalories – This is a function that gets the individual values for a particular day.

You will also notice that the os.system(“clear”) function is used to clear the screen of text between each screen.

Print without newline

Sometimes we will want to use a print inside of a loop to print on one line. We can stop the computer adding a newline after each line iteration of the loop by adding end = “\t” to each print statement. This way, instead of adding a newline, it just adds a tab – great for making tables!

Replacing list items

When we start our program, rather than create an empty list, we create a list that contains 4 empty lists. These empty lists are then later replaced with the lists of values as they are added.

Version 4 – Persistent Data Storage in a file

Our program is now working quite well, but there is a problem! Whenever we close our program, we lose all our data and have to start again… This is no good. What we need is a way to save our data that is stored in the month list. The quickiest and most ‘Pythonesque’ way of doing this is to use pickle.

Using pickle is quite easy and is broken down in to 2 steps.

Step 1 – Loading your data from a pickle file

To load our data from a pickle file is easy-peasy. We just need a couple of lines of code.

Step 2 – Saving your data

Saving pickle files is even easier, just one line of code!

Final Code

Here is the final code!

Improving the program

We now have a fully working program, but there are still a few improvements to make!

  • On the main screen, when we are asked to input the input the week number we want to edit, the input is not validated (checked that it only contains 1,2,3 or 4). We can now write a function (similar to the getCalories() function) that only allows valid input.
  • We could add the option to clear all data from the database.
  • We could add an option to display the monthly average calories consume