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.
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 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!
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