Healthy Eating with Python

Click on the Play button to run the code This blog post was inspired by Mr Rooney's tweet.

Ok so I'm not exactly known for my healthy eating, but it is an important part of our lives. So how can we teach children to eat healthy using Python?

Goal

To use Python 3, lists and the random library to teach healthy eating.

Getting started

Using IDLE / Python 3 Editor or in this case Trinket.io, create a new file and call it Healthy-Eating.py.

Importing Libraries

The only library that we need is called random and as its name suggests, it is a collection of classes and functions that offer random elements (choice, shuffle etc) to our code.

import random  

Creating two lists

Lists are a Pythonic way of handling data in to a single indexable object. A list in Python is known as an array in other languages.
Lists can contain any datatype (strings, integers, floats, other lists) and each item in a list has an index, a numerical value that identifies its position. The index starts at zero. Lists are mutable, in other words the contents can be changed.
We shall create two lists, healthy and unhealthy. Our healthy list is full of healthy food, saved as strings. Our unhealthy list is full of tasty awful food that is bad for you.

healthy = ["carrot","apple","mango","pineapple"]  
unhealthy = ["pizza","burger","kebab","chips"]  

Communicating with the user

Our interface with the user starts by welcoming them to the application by using a print function. You will also notice \n at the end of the text. This is Python's way of adding a new line after the print function.

print("Welcome to Healthy Eating with Python\n")  

Now the user will enter their favourite food, for this we use a variable called food which will capture the keyboard input from the user.

food = input("What is your favourite food? :")  

Check what they are eating

So we now enter into an if...else if...else conditional test. Our first test is to see if the food they are eating is in the healthy list. If it is, then this condition is True and we print a celebratory message to the user. We also suggest they celebrate by eating another, randomly chosen from the healthy list.

if food in healthy:  
    print("Well done you are healthy, celebrate with a "+random.choice(healthy))

Unhealthy eating!

But what if their food is not healthy? Well our else if condition is True. We shuffle the healthy list, to create a pseudo-random list of healthy foods.

elif food in unhealthy:  
    random.shuffle(healthy)

Then we print a message to the user suggesting that they eat something healthy.

    print("Oh dear, you should eat some... ")

Now we suggest three alternative foods to the user. We use a for loop to print the first three items in the healthy list. The value i starts at zero, and each time the for loop iterates, the value of i is increased by 1.

    for i in range(3):
        print(healthy[i]+"\n")

But their food isn't in any off the lists!?!

Our final test is else and this is condition is True if the previous tests are False. So in other words if the user enters a food that is not in either list, then the code inside else will be executed.

else:  

So we tell the user that we cannot find their food in the "database"" (our two lists). We then shuffle the list of healthy foods.

    print("Your food choice is not in my database, but I guess that it is bad so here are some alternatives")
    random.shuffle(healthy)

Then we use another for loop to print the contents of the healthy list. To print everything in the list we set a range, a set number of times to run the for loop. Our range is set to the len, the length of our list. We then print each item in the list, helping the user to eat better.

    for i in range(len(healthy)):
        print(healthy[i]+"\n")

Complete code listing

import random  
healthy = ["carrot","apple","mango","pineapple"]  
unhealthy = ["pizza","burger","kebab","chips"]  
print("Welcome to Healthy Eating with Python\n")  
food = input("What is your favourite food? :")

if food in healthy:  
    print("Well done you are healthy, celebrate with a "+random.choice(healthy))
elif food in unhealthy:  
    random.shuffle(healthy)
    print("Oh dear, you should eat some... ")
    for i in range(3):
        print(healthy[i]+"\n")
else:  
    print("Your food choice is not in my database, but I guess that it is bad so here are some alternatives")
    random.shuffle(healthy)
    for i in range(len(healthy)):
        print(healthy[i]+"\n")

All done!

So there we have it, a simple tool that teaches a little about Python, and encourages healthy eating.

All this talk of food has made me hungry...I'm off to Greggs to eat a lovely apple. (Bacon, pies etc are ok for you in moderation, but don't eat them everyday, oh and try and go for a walk/run/swim in between bouts of coding.)