1
00:00:05,104 --> 00:00:10,795
Alright, so we've seen how to keep our data and code
separate, and we'll do that again for this program.

2
00:00:10,795 --> 00:00:15,920
So what we want to do is create a 
new Python file called meal_planner

3
00:00:24,480 --> 00:00:27,360
Alright, so the first thing we're going
to do in this new program is to import the  

4
00:00:27,360 --> 00:00:36,640
dictionaries from the last video. I'm going to type
from contents import pantry comma recipes.  

5
00:00:36,640 --> 00:00:40,240
We've seen an import like that 
previously, in the simple jukebox program  

6
00:00:40,240 --> 00:00:45,120
back in the Lists and Tuples section. By 
importing the dictionaries from content.py, 

7
00:00:45,120 --> 00:00:49,280
they become available in this program. So I'm going
to check that by printing the dictionaries. 

8
00:00:49,280 --> 00:00:59,362
So let's do that, so print parentheses pantry.
Next line, print parentheses recipes.

9
00:00:59,362 --> 00:01:01,496
Run the program.

10
00:01:02,560 --> 00:01:07,543
We confirm that our dictionaries are available
in this code and it is working.

11
00:01:07,543 --> 00:01:15,449
And we need to scroll the run pane right, as
you can see there - the output - to see everything.

12
00:01:15,449 --> 00:01:18,338
Alright, so so far so good.

13
00:01:18,338 --> 00:01:24,160
Next we need to present the recipes to 
the user so that they can choose one.

14
00:01:24,160 --> 00:01:30,000
I can hear the groans already - not another menu.
Once again, we're not learning how to write a menu -

15
00:01:30,000 --> 00:01:34,960
we're learning how to transform data. We 
display it in a menu, because that lets us see  

16
00:01:34,960 --> 00:01:40,480
what we've done to it. Just printing it out would
serve the same purpose, but it's more interesting  

17
00:01:40,480 --> 00:01:45,040
if there's a use for the transformed 
data. So how do we transform the data?

18
00:01:46,880 --> 00:01:51,200
So transforming our recipe dictionary, the 
dictionary values have been replaced with an  

19
00:01:51,200 --> 00:01:56,000
ellipsis on the right hand side, just so everything
fits on the slide. But you can see that the right

20
00:01:56,000 --> 00:02:03,200
hand side represents our recipes dictionary.
We need to transform that to what we can see on the  

21
00:02:03,200 --> 00:02:08,720
left hand side. In python that's easy, in fact we 
can do it with one line of code. Because that would  

22
00:02:08,720 --> 00:02:12,720
use something that we'll be covering later,
we're going to use three lines of code instead.

23
00:02:13,520 --> 00:02:16,640
Alright, so back to our code. I'm going to 
paste in the one line that I mentioned. 

24
00:02:16,640 --> 00:02:20,400
I'm going to delete the two lines that print out
dictionaries. We've confirmed they're available,  

25
00:02:20,400 --> 00:02:25,440
and that was the only reason that I printed 
them. I'm going to paste in this code.  

26
00:02:25,440 --> 00:02:29,440
Now don't try to make sense of that 
line of code. It's called a dictionary  

27
00:02:29,440 --> 00:02:34,340
comprehension, and we'll be learning 
about comprehensions later in the course.

28
00:02:35,200 --> 00:02:39,760
We have to learn to walk before we can run.
Obviously you can't learn about dictionary  

29
00:02:39,760 --> 00:02:44,800
comprehensions before you've learned about 
dictionaries, but that leaves me with a quandary.

30
00:02:44,800 --> 00:02:49,120
I have to show you code that makes sense 
with what we've covered so far, but I'm also

31
00:02:49,120 --> 00:02:54,480
aware that there's a more efficient and pythonic 
way to write it. To help solve that quandary, I've  

32
00:02:54,480 --> 00:02:58,720
presented the more efficient code as a comment.
When you come to review these lectures, and the  

33
00:02:58,720 --> 00:03:03,840
code samples, that comment will remind you 
that you should use a comprehension here.

34
00:03:05,200 --> 00:03:09,840
And it will make perfect sense once you've 
watched the section with comprehensions. Until

35
00:03:09,840 --> 00:03:13,516
then think, of these comments as
a reminder to your future self.

36
00:03:13,516 --> 00:03:14,912
Alright, so moving on.

37
00:03:14,912 --> 00:03:20,195
Let's see the more naive way of transforming
our recipes into something we can display to the user.

38
00:03:20,195 --> 00:03:24,455
We're going to iterate over the dictionary,
and use the keys to create a new one.

39
00:03:24,455 --> 00:03:27,762
Because we're using that new dictionary
to display the menu to the user,

40
00:03:27,762 --> 00:03:31,211
I'm going to call it display_dict,

41
00:03:31,211 --> 00:03:37,203
so display_dict equals, and left and
right curly braces for an empty dictionary.

42
00:03:37,203 --> 00:03:46,080
On the next line, for index comma key
in enumerate parentheses recipes,  

43
00:03:46,080 --> 00:03:51,520
and outside of the parentheses there's a colon.
So we're creating an empty dictionary on line four.  

44
00:03:51,520 --> 00:03:55,443
Next we're iterating over the keys of the 
dictionary, on line five.

45
00:03:55,443 --> 00:03:58,343
Now we used the enumerate function,
when we were working with lists.

46
00:03:58,343 --> 00:04:03,377
If you checked the documentation for enumerate,
you'll have seen that it can be used with any iterable.

47
00:04:03,377 --> 00:04:06,560
And if you didn't check the documentation, why not? :-)

48
00:04:06,560 --> 00:04:11,440
Alright, so we're iterating over the tuples 
that the enumerate function produces.  

49
00:04:11,440 --> 00:04:15,760
Each tuple will contain an index, and the key
from the dictionary. So let's check what we're  

50
00:04:15,760 --> 00:04:23,549
working with, before we write any more code.
So we'll just print parentheses index comma key.

51
00:04:24,560 --> 00:04:29,913
So what I'm going to do is move 
the run pane. We'll run it first

52
00:04:30,720 --> 00:04:35,690
Alright, so I'm going to move 
this to right top - the run pane -

53
00:04:35,690 --> 00:04:40,640
so we can see the output a bit better. 
It also keeps it clear of the subtitles.  

54
00:04:40,640 --> 00:04:45,280
So the output looks very close to what we 
want. The indices start at zero which isn't  

55
00:04:45,280 --> 00:04:48,880
ideal, but we'll fix that shortly. And if 
you're thinking Hang on, dictionaries don't  

56
00:04:48,880 --> 00:04:54,480
have index numbers, you're right - they don't.
They use the keys to index into a dictionary.  

57
00:04:54,480 --> 00:04:58,560
The enumerate function creates 
the index numbers for us.  

58
00:04:58,560 --> 00:05:04,637
It iterates over an iterable, and generates an index
number for each item it finds, starting at zero. 

59
00:05:04,637 --> 00:05:09,841
It then returns tuples, containing the index
and value for each item in the iterable.  

60
00:05:09,841 --> 00:05:16,960
So we're unpacking each tuple in turn, on line 5,
to get the index numbers for each entry in the dictionary.  

61
00:05:16,960 --> 00:05:21,280
If you're still a bit unsure about what we've 
done here, I'll explain it again with a slide.  

62
00:05:21,280 --> 00:05:25,600
First, we're going to display our menu repeatedly
in a loop, so I'll store these values in  

63
00:05:25,600 --> 00:05:31,040
our display_dict dictionary, instead of 
printing them. I'm going to delete this print line  

64
00:05:31,040 --> 00:05:38,720
and replace that with display_dict,
and square bracket str parentheses  

65
00:05:38,720 --> 00:05:46,880
index, plus one in those parentheses, 
closing square bracket then equals key.

66
00:05:46,880 --> 00:05:51,280
So I've added one to each index because we don't
want the menu to start at zero. I've also converted  

67
00:05:51,280 --> 00:05:57,440
the indexes to strings, on line six. Our users
will be entering strings and it's much easier to  

68
00:05:57,440 --> 00:06:02,800
convert those integer indexes to strings.
If we didn't do that, we'd have to convert the user's  

69
00:06:02,800 --> 00:06:07,760
input to integers, and we've seen that causes
problems if they type in letters. If you wanted  

70
00:06:07,760 --> 00:06:12,800
index keys for some reason, then we've written
a get_integer function that you could use.  

71
00:06:12,800 --> 00:06:18,178
But for this application, it's actually easier 
to convert the index numbers to strings.  

72
00:06:18,178 --> 00:06:22,480
Okay, so before we see the slides I promised,
let's print out this new dictionary,  

73
00:06:22,480 --> 00:06:27,920
so that we're sure of what we've created. 
We're going to do that inside the while loop.  

74
00:06:27,920 --> 00:06:33,040
Starting on line 8, I'm going to put while True

75
00:06:33,040 --> 00:06:39,000
and a comment here to display a menu 
of the recipes we know how to cook.

76
00:06:43,920 --> 00:06:50,080
I'll close down the output. I'm going 
to print parentheses double quotes

77
00:06:52,240 --> 00:06:56,407
Please choose your recipe,

78
00:06:57,280 --> 00:07:05,920
and we'll just put some dash characters
as a heading. The actual code next; for  

79
00:07:05,920 --> 00:07:16,637
key comma value in display_dict dot 
items parentheses colon, and print parentheses  

80
00:07:16,637 --> 00:07:28,160
f double quotes and key in curly braces, dash
then value in curly braces, and we finish off that line.

81
00:07:28,160 --> 00:07:35,520
On line 15, indented to the same level 
as the for loop, choice is equal to input  

82
00:07:35,520 --> 00:07:41,680
parentheses double quotes, colon and a 
space in those double quotes. Then on line 17,  

83
00:07:41,680 --> 00:07:49,130
if choice is equal to zero, two 
equal signs there, colon break.

84
00:07:51,200 --> 00:07:54,160
So we've seen code like that before - 
we're iterating over the items of  

85
00:07:54,160 --> 00:07:59,251
a dictionary, to display the keys and values.
Our display_dict dictionary

86
00:07:59,251 --> 00:08:03,520
has numeric characters as 
its keys, and the recipes as its values.  

87
00:08:03,520 --> 00:08:10,800
What may not be obvious, is that its values are
actually the keys from the recipe's dictionary

88
00:08:10,800 --> 00:08:14,640
So we've transformed the data,
so that the keys from recipes  

89
00:08:14,640 --> 00:08:20,720
becomes the values in display_dict.
The keys in our recipes dictionary, on the left,  

90
00:08:20,720 --> 00:08:25,840
have become the values for the
display_dict dictionary on the right.

91
00:08:26,800 --> 00:08:31,600
The enumerate function takes each of the keys from 
the dictionary, generates the next number for each  

92
00:08:31,600 --> 00:08:37,760
one, and emits both values as a tuple. Our code then 
takes the first item in each tuple, which is the  

93
00:08:37,760 --> 00:08:43,200
number and converts it to a string. It uses that 
string as the key in the display_dict

94
00:08:43,200 --> 00:08:49,120
dictionary. It uses the second item in the 
tuple, the recipe name. as the value for that key.

95
00:08:51,040 --> 00:08:55,920
We end up with a new dictionary, the
display_dict dictionary, shown on the right.

96
00:08:55,920 --> 00:08:59,760
All the values on the right are valid 
keys in the recipes dictionary, on the

97
00:08:59,760 --> 00:09:06,240
left. That means we can use those values 
to access items in the recipes dictionary.

98
00:09:06,960 --> 00:09:11,360
If the user chooses option 3 for a pizza,
we can look up pizza in the recipes

99
00:09:11,360 --> 00:09:19,600
to get the list of ingredients for a pizza.
So we'll see that working in the next video.

