1
00:00:05,200 --> 00:00:10,363
Alright, so back to our meal planner code.
It's time to make these dictionaries work together.  

2
00:00:10,363 --> 00:00:16,261
At the moment, our code creates the display_menu
dictionary, and uses it to print the menu.

3
00:00:16,261 --> 00:00:20,560
Now we're getting input from the user, but 
only checking for zero at the moment, and you can  

4
00:00:20,560 --> 00:00:26,320
see that's on line 17. If the user enters zero,
we break out of the loop. If they enter something else,  

5
00:00:26,320 --> 00:00:34,183
we'll start by making sure it's a valid choice.
So we'll come down here and start typing on line 19.  

6
00:00:34,183 --> 00:00:43,638
We'll put an elif here - that's obviously for 
our if - and we're going to do a choice in display_dict colon. 

7
00:00:43,638 --> 00:00:46,880
I'll just move this up a bit, so
we're not affecting the subtitles.  

8
00:00:46,880 --> 00:00:55,960
Then we're going to put selected_item is 
equal to display_dict square brackets choice. 

9
00:00:55,960 --> 00:01:02,632
Then we're going to print it out, so print 
parentheses f double quotes You have selected.  

10
00:01:02,632 --> 00:01:08,666
Then in left and right curly braces,
selected_item, and finish the line. 

11
00:01:08,666 --> 00:01:14,320
Alright, so the condition on line 19, checks 
that the user's input is a valid key  

12
00:01:14,320 --> 00:01:21,181
in the display_dict dictionary. If it is, we can
proceed to check the ingredients, and we'll do that next. 

13
00:01:21,181 --> 00:01:26,640
Line 20 uses the key to get a value
from the display_dict dictionary,  

14
00:01:26,640 --> 00:01:31,520
and we're printing that value out in the message
on line 21. That's useful confirmation for the user,  

15
00:01:31,520 --> 00:01:37,613
and also lets us see what's happening. 
Alright, so let's run the program.

16
00:01:38,560 --> 00:01:44,878
Alright, so I fancy an egg sandwich. 
I'm going to select option four.

17
00:01:46,560 --> 00:01:50,320
An egg sandwich appears, as you can see, in
the message, You've selected Egg sandwich,  

18
00:01:50,320 --> 00:01:55,680
so that's working well. Egg sandwich is
a value in our display_dict dictionary.  

19
00:01:55,680 --> 00:02:02,160
It's also a key in the recipes dictionary. I'm 
going to switch back to contents.py over here.  

20
00:02:02,160 --> 00:02:10,320
We'll end the program, and you can see there's
the key, on line 50. Intellij has a useful feature  

21
00:02:10,320 --> 00:02:14,880
that can make it easier to work with large 
data structures. So notice that each of the  

22
00:02:14,880 --> 00:02:20,960
recipe keys has a little minus button, just on the
margin. Clicking that button collapses the entry.  

23
00:02:20,960 --> 00:02:25,843
The button then changes to a little 
plus, which will expand the entry again.

24
00:02:34,320 --> 00:02:40,240
Alrighty. So you can see I've collapsed each of the
keys in our recipes dictionary, to remove some of  

25
00:02:40,240 --> 00:02:45,480
the clutter from the screen. Now the line numbers
in the margin remain correct. We've still got egg sandwich,  

26
00:02:45,480 --> 00:02:50,640
as you can see there on line 50, with beans 
on toast appearing on line 55. We're just hiding  

27
00:02:50,640 --> 00:02:57,190
the details, to make it easier to read the keys. Alright,
so the value we chose from the display_dict dictionary  

28
00:02:57,190 --> 00:03:02,240
can be used to get the list of ingredients
from this recipes dictionary.  

29
00:03:02,240 --> 00:03:09,562
So I'm going to expand egg sandwich again, and
there's the ingredients we need on lines 51 through 53. 

30
00:03:09,562 --> 00:03:15,000
So let's get that list in our code. 
We're going to switch back to meal_planner,

31
00:03:15,000 --> 00:03:21,200
and we're going to print that list out. So let's
come down here, and it's still within our elif block.  

32
00:03:21,200 --> 00:03:25,777
After the print, we're going to retrieve the 
list from the recipes dictionary. What I'll do is, 

33
00:03:25,777 --> 00:03:30,173
I'll print it to check what we're getting, 
but we'll do something more interesting with it soon.

34
00:03:30,173 --> 00:03:36,271
So on the next line I'm going to start 
with print parentheses checking ingredients

35
00:03:37,520 --> 00:03:43,787
dot dot dot, and then on the next line, 
we're going to type ingredients  

36
00:03:43,787 --> 00:03:49,683
is equal to recipes, left square bracket, 
then selected_item.   

37
00:03:49,683 --> 00:03:55,177
And just to make sure, as I mentioned,
we're going to print ingredients.

38
00:03:56,080 --> 00:04:00,630
Alright. Now I did actually stop the 
program, so I'm going to run it again.  

39
00:04:01,360 --> 00:04:07,120
I'm going to choose option 4 again. So after
the You've selected egg sandwich message, we've  

40
00:04:07,120 --> 00:04:11,760
got checking ingredients, and we can also now
see our list of ingredients. So make sure you  

41
00:04:11,760 --> 00:04:16,240
understand what's happening here. The next step
is to link in the pantry dictionary, and if you  

42
00:04:16,240 --> 00:04:20,560
don't understand this bit, you'll really struggle
with the next step. Now this is something that  

43
00:04:20,560 --> 00:04:27,073
students have found confusing, so I'm going to labour the point
here. I'll use some slides  to review what's happening here.

44
00:04:27,073 --> 00:04:34,896
So we saw this in the last video. We use enumerate to
take the keys from the recipe's dictionary

45
00:04:34,896 --> 00:04:39,600
and produce a new dictionary
called display_dict.

46
00:04:39,840 --> 00:04:45,280
That lets us display the keys, with an option
number that the user can use to select a recipe.

47
00:04:46,080 --> 00:04:49,480
Now let's see what happens 
when the user makes a selection.

48
00:04:50,640 --> 00:04:55,040
I've used option 4 for this example, because that's
the option I used when we ran the program earlier.

49
00:04:55,760 --> 00:05:00,640
Option four is the egg sandwich, and we retrieve 
that value from the display_dict  

50
00:05:00,640 --> 00:05:07,280
dictionary. We can then use that value to 
retrieve the ingredients list from recipes.

51
00:05:07,280 --> 00:05:12,640
That's the blue arrow on the slide. When we have a 
value from the display_dict dictionary,

52
00:05:12,640 --> 00:05:21,120
we can use it to index into the recipes dictionary.
Because all the values on the right were produced

53
00:05:21,120 --> 00:05:26,000
from the keys on the left, we know they'll 
work as indexes into the recipe's dictionary.

54
00:05:26,640 --> 00:05:32,560
The pale red arrow shows where Egg sandwich came 
from. We didn't retype those strings, so we don't

55
00:05:32,560 --> 00:05:38,560
have to worry about things like typing errors,
missing spaces or incorrect capitalization.

56
00:05:38,560 --> 00:05:45,440
So spend some time making sure you understand
this, before moving on to the next video.

