1
00:00:05,360 --> 00:00:09,120
At the end of the last video, I mentioned
that our program doesn't tell us how much

2
00:00:09,120 --> 00:00:14,800
of each ingredient we need. The problem is the
recipes dictionary. Let's have a look at that,

3
00:00:14,800 --> 00:00:21,520
in contents.py. So each recipe contains
a list of ingredients. However, as you can see,

4
00:00:21,520 --> 00:00:27,760
there's no quantities. Adding the quantities is
easy - it's just a bit of typing. Deciding how to

5
00:00:27,760 --> 00:00:34,080
add them is harder. So how are we going to store the
recipe items, if we want to include the quantities.

6
00:00:34,080 --> 00:00:40,080
There are two obvious options; we could store
them as tuples; or as key value pairs - a dictionary,

7
00:00:40,080 --> 00:00:45,200
in other words. I'll open up a Python file
that I've created and added to our project,

8
00:00:45,200 --> 00:00:51,360
with two different dictionaries. You can see it's
recipe_options.py. Now you can download the

9
00:00:51,360 --> 00:00:56,720
file from the resources for this video. Download
recipe_options.txt then copy and paste

10
00:00:56,720 --> 00:01:01,680
into a new Python file, as I've already done. And
I've called mine recipe_options.py, as

11
00:01:01,680 --> 00:01:07,520
you can see there. Now to keep things simple,
I've only included one recipe in each dictionary;

12
00:01:07,520 --> 00:01:12,720
chicken and chips, and you can see those on screen.
The first one, recipes_tuple, the lines

13
00:01:12,720 --> 00:01:19,280
one through eight, uses a list of tuples. The
value for the key chicken and chips is a list.

14
00:01:19,280 --> 00:01:25,920
Inside the list we've got four tuples. Each tuple,
lines three to six, contains an ingredient name

15
00:01:25,920 --> 00:01:31,680
and the quantity of that ingredient. Below that,
we've got our recipes_dict on lines 10

16
00:01:31,680 --> 00:01:37,360
to 17. The value for the key chicken and chips
is a dictionary. Within that dictionary, we've got

17
00:01:37,360 --> 00:01:44,960
key value pairs. The value for the key chicken,
is 100. This recipe needs 100 grams of chicken.

18
00:01:44,960 --> 00:01:50,000
Now don't worry too much about the units, by the
way. In America, you'd probably be referring to

19
00:01:50,000 --> 00:01:56,800
ounces and cups. Many other places will use grams
and millilitres. In the UK, they use whatever they

20
00:01:56,800 --> 00:02:02,640
feel like. They buy beer in pints, petrol in
litres, and measure fuel consumption in miles

21
00:02:02,640 --> 00:02:07,440
per gallon. We should really be storing the units
somehow, so that our program can convert them

22
00:02:07,440 --> 00:02:14,480
into whatever the cook is used to, but I don't
want to make things too complicated at this stage.

23
00:02:14,480 --> 00:02:20,320
So how to choose? Okay, so how do we decide which
of these data structures will be more appropriate

24
00:02:20,320 --> 00:02:24,720
for our program. This can have new
programmers staring at the screen for hours,

25
00:02:24,720 --> 00:02:30,240
unable to proceed. They know they should
use one of them, but can't decide which.

26
00:02:30,240 --> 00:02:34,800
If you find yourself in that position, just
pick one. Toss a coin, if you like. It's much

27
00:02:34,800 --> 00:02:40,560
better to start doing something, than to sit
there in a state of paralysed indecision.

28
00:02:40,560 --> 00:02:46,880
Trust me - if it matters, and you choose
the wrong one, you'll soon find out.

29
00:02:46,880 --> 00:02:51,120
Now please note that I'm not suggesting
you design your programs by tossing a coin.

30
00:02:51,120 --> 00:02:56,240
Think carefully about the requirements, and what
you'll need to do with your data structures

31
00:02:56,240 --> 00:03:00,720
but if you can't decide between different
alternatives, then picking one and getting

32
00:03:00,720 --> 00:03:07,840
on with it is much better than doing nothing. If
you do nothing, that's what you'll learn - nothing!

33
00:03:07,840 --> 00:03:12,400
If you make a mistake and make the wrong choice,
you'll have learnt that it is the wrong choice.

34
00:03:12,400 --> 00:03:17,360
And hopefully, you'll also have learnt why.
Here though, it doesn't really matter. 

35
00:03:17,360 --> 00:03:23,120
I'll go back to our code, and we'll see what getting
the item and quantity looks like, in each case.

36
00:03:23,120 --> 00:03:27,200
Alright, so let's scroll down and look at the
code that I've added at the bottom of this recipe

37
00:03:27,200 --> 00:03:32,480
underscore options.py file. Now it might take
you a moment to spot the difference. If I hadn't

38
00:03:32,480 --> 00:03:41,040
included the comments on lines 22 and on lines
30, spotting it would be even harder. On line 22,

39
00:03:41,040 --> 00:03:45,120
we unpacked the tuple that we retrieved
from the list. You can see each ingredient

40
00:03:45,120 --> 00:03:52,400
and quantity that we unpack, in the output,
when we run the program, which I'll do now.

41
00:03:52,400 --> 00:03:58,800
Now on line 30, we iterate over the dictionary's
items. That returns the key value pairs as

42
00:03:58,800 --> 00:04:07,200
tuples, and we unpack them in the same way. In both
cases, the output is the same, which we'll run again.

43
00:04:07,200 --> 00:04:12,629
So hopefully, that'll give you an idea of why
it was difficult to choose between these two approaches - 

44
00:04:12,629 --> 00:04:17,440
either one will work fine. We don't
need to make changes to the recipe while the

45
00:04:17,440 --> 00:04:22,640
code is running, so we can use either of these data
structures. That's not always the case, though. 

46
00:04:22,640 --> 00:04:30,320
Let's go back to contents.py. Let's have a look at the
pantry dictionary, on lines 1 through 21. When we

47
00:04:30,320 --> 00:04:34,960
tell the program that we're going to make a recipe,
the program will subtract the amount of each

48
00:04:34,960 --> 00:04:41,280
ingredient used. We are going to be updating the
values in pantry. Using tuples here wouldn't work,

49
00:04:41,280 --> 00:04:47,040
because tuples are immutable. But for our recipes,
when including the quantities, we can use either.

50
00:04:47,040 --> 00:04:52,320
Now this is a section on dictionaries, so the
obvious one for me to choose is a dictionary. 

51
00:04:52,320 --> 00:04:57,200
Now you don't want to type all the quantities into our
recipes dictionary, so download the file contents

52
00:04:57,200 --> 00:05:02,720
contents_quantities.txt from the resources
for this video. So copy all the text, and paste

53
00:05:02,720 --> 00:05:09,840
it into contents.py, replacing all the code that's
already in there. So I'm going to do that right now.

54
00:05:12,400 --> 00:05:14,800
Alright, with that done, we're now
ready to get our program to check

55
00:05:14,800 --> 00:05:19,680
the quantity of each ingredient.
We'll do that in the next video.

