1
00:00:05,280 --> 00:00:10,720
Alright, so we've now got quantities in our
pantry, so we know how much of each item we've got.

2
00:00:10,720 --> 00:00:17,600
We need to change the code in meal_planner.py,
to check those quantities. Before we do,

3
00:00:17,600 --> 00:00:22,880
remember that we might not have that ingredient at
all. It may not exist in the dictionary. If we try

4
00:00:22,880 --> 00:00:28,960
to retrieve the quantity, using the ingredient name
as a key, we'll get an exception in that scenario,

5
00:00:28,960 --> 00:00:33,920
and we saw that right at the start of this section.
This is a good chance to see when you'd want to

6
00:00:33,920 --> 00:00:39,760
use get, rather than indexing into the dictionary.
I'm going to start by changing for loop

7
00:00:39,760 --> 00:00:45,840
on line 25. As you can see at the moment,
it's for food_item in ingredients.

8
00:00:45,840 --> 00:00:56,218
We're going to change that to for
food_item comma required_quantity

9
00:00:56,218 --> 00:01:03,280
in ingredients, and we'll put dot items parentheses
on the end of that. So what we're doing here is

10
00:01:03,280 --> 00:01:08,640
we're iterating over the items, and unpacking
the key and value. That'll give us the quantity

11
00:01:08,640 --> 00:01:14,720
for each ingredient. Remember that ingredients is
now a dictionary. We changed that at the end of the

12
00:01:14,720 --> 00:01:21,280
last video. Looking at contents.py again,
so the example there, lines 24 to 40, butter chicken

13
00:01:21,280 --> 00:01:28,160
is a dictionary. Now back in meal_planner, we should
now have the ingredient name and the quantity 

14
00:01:28,160 --> 00:01:35,200
we need for the recipe. At the moment, line 26 checks
that the food item is in the pantry dictionary.

15
00:01:35,200 --> 00:01:40,400
We need to do more than that - we need to find out
how much we have in our pantry. So let's add a line

16
00:01:40,400 --> 00:01:50,767
of code, after our for loop. I'm going to type here,
quantity_in_pantry is equal to

17
00:01:50,767 --> 00:01:59,920
pantry.get, and in parentheses, we're going
to add food_item comma 0. As I said, we

18
00:01:59,920 --> 00:02:05,920
might not have that item at all. If we use the
food_item variable as an index into pantry,

19
00:02:05,920 --> 00:02:12,160
our program could crash. Instead, we pass
food_item to the get method, to retrieve the

20
00:02:12,160 --> 00:02:16,960
quantity_in_pantry. So we're putting the
value in there. So if the item doesn't

21
00:02:16,960 --> 00:02:22,560
exist, get will return that default value 0 instead
of crashing. Using a default value with get is very

22
00:02:22,560 --> 00:02:28,160
useful. We now know how much of that ingredient we
have, including if we don't have any of it. Alright, so

23
00:02:28,160 --> 00:02:33,200
we now need to change line 27. Instead of
if food_item in pantry, we're going to

24
00:02:33,200 --> 00:02:45,200
change that to be if required_quantity
is less than or equal to quantity_in_pantry colon.

25
00:02:45,200 --> 00:02:49,440
So if the quantity we need is less than or
equal to the amount that we have in the pantry,

26
00:02:49,440 --> 00:02:53,680
then we've got enough, and we'll print the OK
message, as we're doing on line 28. What we

27
00:02:53,680 --> 00:02:59,120
can do now is improve the else block. It would be
more useful if it told us how much we need to buy.

28
00:02:59,120 --> 00:03:05,920
Let's have a look at. So on line 28,
I'm going to type quantity_to_buy

29
00:03:05,920 --> 00:03:12,960
is equal to required_quantity
minus quantity_in_pantry.

30
00:03:12,960 --> 00:03:20,080
Let's replace this line here with the code I'm
about to type in; print parentheses, f for an f

31
00:03:20,080 --> 00:03:28,160
string, double quotes backslash t You need
to buy, then in left and right curly braces,

32
00:03:28,160 --> 00:03:35,280
quantity_to_buy, closing right curly
brace, of. Then the item, which will

33
00:03:35,280 --> 00:03:42,000
be in curly braces, food_item. That tells
us what we need to buy; the quantity, and the actual

34
00:03:42,000 --> 00:03:51,840
item of food as well. So let's run the program now,
and we'll start off by choosing 1: Butter chicken.

35
00:03:51,840 --> 00:03:55,760
As you can see, the messages are more useful
now. This tells us how much chicken to buy, at

36
00:03:55,760 --> 00:04:03,040
the top of the screen there, and how much
ginger and tomato puree we need as well.

37
00:04:03,040 --> 00:04:07,840
The output is also more accurate. If you remember,
Butter chicken was one of the recipes that our

38
00:04:07,840 --> 00:04:12,800
program told us we could make. It checked that we
had each ingredient, but it didn't check how much

39
00:04:12,800 --> 00:04:18,480
we had. It's much more useful now - it makes sure
that we have enough of each ingredient. I'm not

40
00:04:18,480 --> 00:04:22,720
sure that we should be printing the ingredients
that are OK. They clutter the output and detract

41
00:04:22,720 --> 00:04:28,560
from the ingredients we need to buy. But that's more
of an observation than a definite objection.

42
00:04:28,560 --> 00:04:34,298
Including the OK items allows our cook to
check that they really do have those ingredients.

43
00:04:35,200 --> 00:04:40,320
If someone came down for a midnight snack and
ate the yogurt, our program might not know about it.

44
00:04:40,320 --> 00:04:45,440
If you want, you could use the colorama module, to
print the output in different colours. Pay attention

45
00:04:45,440 --> 00:04:50,640
to what you display to your users, though. Sometimes,
too much information can make your user miss

46
00:04:50,640 --> 00:04:56,240
important details. Alright, so now we're starting to
get to a useful program. At the moment, though, it only

47
00:04:56,240 --> 00:05:02,932
produces output on the screen. Our cook will either
have to write things down or print the output.

48
00:05:03,520 --> 00:05:08,160
That's just not good enough these days.
Users expect more from their technology.

49
00:05:08,160 --> 00:05:13,360
Our program could order the items from an online
food store, or send the details to a mobile phone.

50
00:05:13,360 --> 00:05:18,320
That would be what modern users would expect.
We're not going to do all of that straight away,

51
00:05:18,320 --> 00:05:22,240
but you will do the first step. Whatever
we decide to get the program to do,

52
00:05:22,240 --> 00:05:27,485
is going to need the data in a more
usable form. So it's time for a challenge.

53
00:05:29,360 --> 00:05:33,760
At the moment, our program prints out the
items that we need to buy. Just printing

54
00:05:33,760 --> 00:05:38,080
to the screen isn't very useful, unless you
want to write it down or send it to a printer,

55
00:05:38,080 --> 00:05:44,480
and take bits of paper when you go shopping. That's
so, quote unquote, last century. Modify the program,

56
00:05:44,480 --> 00:05:50,960
so that it puts the items we need to buy into some
sort of data structure. It's entirely up to you

57
00:05:50,960 --> 00:05:57,268
what type of structure you use. You may decide
to create some sort of list or a dictionary.

58
00:05:58,080 --> 00:06:02,720
The important thing is, that you must
produce something that can be iterated over,

59
00:06:02,720 --> 00:06:05,760
to retrieve the ingredient and quantity.

60
00:06:05,760 --> 00:06:10,560
As a hint here, we did something similar with
the buy_computer_dict

61
00:06:10,560 --> 00:06:16,400
program, earlier in this section. And another hint:
whatever data structure you choose, use a function

62
00:06:16,400 --> 00:06:24,400
to add the items to it. So have a go, and we'll
go over the solution to this in the next video.

