This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 8: Real world applications

Challenge 2 - Grocery shopping

face Josiah Wang

Here is a (modified) receipt from one of Josiah’s grocery shopping trips. Download receipt.txt.

Task 1

First load the content of the receipt from the file.

Using regular expressions, extract each of the item name and corresponding price and represent each as a tuple. You should end up with a list of tuples.

groceries = [('W MILK 4 PT', 1.09), 
             ('CANNED FRUIT', 1.0), 
             ('FRESH CREAM', 1.85), 
             ('BLUEBERRIES', 1.56), 
             ('GARLIC', 0.78), 
             ('SALAD', 0.6), 
             ('ICE LOLLIES', 2.0), 
             ('FROZEN VEG', 1.1), 
             ('BM MADELEINES', 2.0)]

Task 2

Using regular expressions, extract the total as listed on the receipt.

Task 3

Compute the total by adding the prices of all items (no regular expressions needed!)

Make sure that the total computed by the supermarket matches the total you computed (definitely no regular expressions needed either!)