1
00:00:05,360 --> 00:00:09,040
Alright, so let's have a look at the
solution to the dictionary menu challenge

2
00:00:09,040 --> 00:00:13,920
from the last video. So how did you get on with the
challenge? So as I mentioned in the iterating over

3
00:00:13,920 --> 00:00:19,440
a dictionary lecture, you can use the dot items
method to get the keys and values at the same time.

4
00:00:19,440 --> 00:00:24,720
That's more efficient than iterating over the
keys, and indexing into the dictionary again 

5
00:00:24,720 --> 00:00:28,960
to get the values. So let's have a look at how to do
that. What I'm going to do is start by adding an

6
00:00:28,960 --> 00:00:33,793
else block and then print a short message.
So I'm going to add the else block, here on line 14,

7
00:00:33,793 --> 00:00:43,840
else colon. Then on line 15, print parentheses
double quotes Please add options from the list.

8
00:00:46,960 --> 00:00:50,720
Alright, so far so good. Next what we're
going to do is iterate over the items and

9
00:00:50,720 --> 00:00:57,680
print the keys and values. So I'm going
to type, on line 16, for key comma value

10
00:00:57,680 --> 00:01:04,400
in available_parts.items with
parentheses there, colon,

11
00:01:06,000 --> 00:01:15,040
and print parentheses f double quotes left and
right curly braces key colon and a space, 

12
00:01:15,040 --> 00:01:22,080
left and right curly braces again, for value - so printing
out both key and value in an f-string. Then outside

13
00:01:22,080 --> 00:01:30,653
of the for loop, indented back one level, I'm
going to print zero colon finish, or to finish.

14
00:01:32,400 --> 00:01:37,920
Alright and I'll push down that other output
down to line 20. So now if we enter an invalid

15
00:01:37,920 --> 00:01:44,394
option, the menu will be printed. When we enter the
loop for the first time, current_choice

16
00:01:44,394 --> 00:01:48,160
isn't a valid option - you can see it's set
to None on line 9. So that means the program

17
00:01:48,160 --> 00:01:53,840
should start by printing the menu. So let's run
the program and confirm if that is the case,

18
00:01:56,000 --> 00:01:58,400
and you see, good news that it is
working. And what I'm going to 

19
00:01:58,400 --> 00:02:03,243
do is move the run pane over to the
right, using the cog icon over here,

20
00:02:06,960 --> 00:02:12,400
like so, and just move that over a little bit. Okay.
Alright so that's looking good - we've got our

21
00:02:12,400 --> 00:02:17,840
seven items printing as a menu and zero to quit,
showing on the screen as well. We're not adding the

22
00:02:17,840 --> 00:02:22,640
items to a shopping cart yet, but we can check that
the correct items are being chosen. So I'm going to

23
00:02:22,640 --> 00:02:31,280
come over here to the run pane, and select 2
for monitor, add a dvd drive, which is option 6,

24
00:02:31,280 --> 00:02:36,659
and anything that isn't a valid choice will
cause the menu to be re-printed. We'll put a 9 there -

25
00:02:36,659 --> 00:02:42,071
we get our menu showing again. Alright,
we'll terminate the program by selecting zero

26
00:02:42,071 --> 00:02:45,840
Alright so that's working. The next step here now,

27
00:02:45,840 --> 00:02:50,381
is to add the items to a list or a dictionary.
Because this section is about dictionaries, 

28
00:02:50,381 --> 00:02:55,680
that's what I'll use. But you could take the existing code
from my original buy_computer program,

29
00:02:55,680 --> 00:03:00,800
and add the items to a list, if you want to try that.
If you remember, choosing an item again removes it

30
00:03:00,800 --> 00:03:05,280
from the list. But we have a challenge at the
end of the video, to do just that. But we need a

31
00:03:05,280 --> 00:03:10,575
copy of this file - buy_computer_dict,
because we're about to make some changes to it.

32
00:03:10,575 --> 00:03:15,280
So I'm going to open the project pane and then
right click buy_computer_dict

33
00:03:15,280 --> 00:03:22,480
and choose Copy, and Copy again. Then I'm going to right
click over here on the project and choose Paste.

34
00:03:22,480 --> 00:03:28,800
I'm going to call this one dict_list.py.
You'll use that for the

35
00:03:28,800 --> 00:03:32,960
challenge at the end of this video. Until
then, we'll close that video in the editor.

36
00:03:33,920 --> 00:03:39,840
So we're back at buy_computer_dict.py.
So I'll create a dictionary to

37
00:03:39,840 --> 00:03:44,480
store the parts that the user chooses, in the
next video. Before that, I'll finish this video

38
00:03:44,480 --> 00:03:51,520
by discussing lines 11 and 12. So on line 11, we're
checking that the chosen part is in the dictionary.

39
00:03:51,520 --> 00:03:58,560
That means we don't have to use get on line 12.
We can obtain the part name by indexing into our

40
00:03:58,560 --> 00:04:03,840
available_parts dictionary, using
current_choice as the key. We know the

41
00:04:03,840 --> 00:04:09,200
code won't crash, because we've already checked
that the key does exist. We did that on line 11.

42
00:04:09,200 --> 00:04:12,720
Another thing worth mentioning, is
that this code is a bit shorter

43
00:04:12,720 --> 00:04:19,725
than the version that used a list for the menu.
So if we go back to our buy_computer.py.

44
00:04:19,725 --> 00:04:25,000
So looking at the code there, we had to create
a list of the valid choices - that's the code on

45
00:04:25,000 --> 00:04:33,040
lines 9 through 12. We need that, to check if the user's
choice is a valid index position into the list.

46
00:04:33,040 --> 00:04:39,658
But in this new version, buy_computer_dict,
the choices are the keys in the dictionary.

47
00:04:39,658 --> 00:04:44,566
So there's no need to create another list, because
we can check if something is in the dictionary.

48
00:04:44,566 --> 00:04:49,760
Now we want to map the user's choice
to an available part. If you remember, I mentioned

49
00:04:49,760 --> 00:04:56,960
that dictionaries are sometimes called mappings.
So in our dictionary, on lines 1 through 7, the character 1

50
00:04:56,960 --> 00:05:03,120
maps to a computer, 2 is mapped to a monitor,
and so on. A dictionary is a good choice here,

51
00:05:03,120 --> 00:05:08,560
because it automatically maps the choices to the
items. So you might be wondering why I bothered

52
00:05:08,560 --> 00:05:13,680
showing you how to use a list, back in the Lists
and Tuples section. Why do I show you one way, when I

53
00:05:13,680 --> 00:05:19,654
know there's a quote unquote better way available?
Well the answer is that you may need to use both.

54
00:05:20,560 --> 00:05:24,640
So if you were writing a shopping cart for
an online store, you wouldn't have the items

55
00:05:24,640 --> 00:05:29,920
typed into your code. The items available for
sale might come from a database, or a file on

56
00:05:29,920 --> 00:05:35,280
disk. If the items appear in this format, you can
see on screen there, the number a comma then the

57
00:05:35,280 --> 00:05:40,000
description, then it would be quite easy to
create a dictionary by reading in that data.

58
00:05:42,240 --> 00:05:45,280
But what if the items were presented
to you without the numbers?

59
00:05:45,280 --> 00:05:49,040
So you might read in a series of items, like you
can see on screen; just the descriptions themselves.

60
00:05:49,840 --> 00:05:53,840
So in that case you'd have to generate the
numerical characters that the user would type.

61
00:05:54,720 --> 00:05:59,760
If you've got to include that code anyway, then it
might be easier to leave the items in a list.

62
00:05:59,760 --> 00:06:05,760
There's another reason why you might prefer to use
a list; Python can sort lists easily. Right so I'm

63
00:06:05,760 --> 00:06:10,160
going to finish this video by demonstrating that,
with the original buy_computer program.

64
00:06:11,920 --> 00:06:20,160
Alright, so I'm clicking on
buy_computer.py here. Now when I run this,

65
00:06:20,160 --> 00:06:24,320
the items are appearing in an unsorted way,
to the right hand side of the screen there.

66
00:06:24,320 --> 00:06:30,000
But humans can find things faster if items are sorted.
So as you can see, the dvd drive appears last,

67
00:06:30,000 --> 00:06:35,200
when it should really appear after computer.
So that's easy to fix. We can sort the list

68
00:06:35,200 --> 00:06:39,669
before using it. So we're going to
enter 0 to terminate the program,

69
00:06:42,160 --> 00:06:48,346
and let's make a change. So what I'm going to
do is come down here, before the while loop. 

70
00:06:48,346 --> 00:06:55,520
Specifically on line 16, I'm going to type
available_parts.sort, leaving the parentheses in,

71
00:06:55,520 --> 00:07:03,030
and I'll just put a line break between that and
the while loop. So if we run the program again now,

72
00:07:03,030 --> 00:07:07,760
you can see the parts appear in alphabetical
order. So you can iterate over a dictionary,

73
00:07:07,760 --> 00:07:13,040
in sorted order of the keys, but it's not
easy or efficient to do that with the values.

74
00:07:13,040 --> 00:07:18,845
More importantly, if we did rearrange the
dictionary, option 6 would appear after option 1,

75
00:07:18,845 --> 00:07:23,896
in second place in the menu. Now obviously,
with our code, we could have typed 1 to 6 

76
00:07:23,896 --> 00:07:29,840
in alphabetical order, but if the data came from an
external source, we wouldn't be able to do that.

77
00:07:30,800 --> 00:07:35,760
So we've seen how to use a list and a dictionary
to present our menu. So which one is quote unquote

78
00:07:35,760 --> 00:07:41,600
better depends on the nature of your original data,
and exactly what you want to do with that data.

79
00:07:41,600 --> 00:07:47,120
We've seen some examples of why a dictionary might
be more suitable here - shorter code for instance.

80
00:07:47,120 --> 00:07:51,200
But we've also seen that a list may be more
appropriate, if you have to sort the values.

81
00:07:51,920 --> 00:07:56,400
So in the next video we'll return to our new
program, and add items that the user chooses.

82
00:07:56,960 --> 00:08:03,840
Before that though, try this challenge. It's very
common to use both lists and dictionaries in the

83
00:08:03,840 --> 00:08:09,440
same program. Have a go at adding and removing
items from a computer_parts list

84
00:08:09,440 --> 00:08:15,200
and I'll add my code as a document after this
video. So remember to use the dict_list.py

85
00:08:15,200 --> 00:08:20,480
file that we copied earlier. The new code
that you'll add will be very similar to the code

86
00:08:20,480 --> 00:08:25,104
we've already got, in the original
buy_computer.py program.

87
00:08:25,104 --> 00:08:29,207
Check out my solution using a list to do that,
in the document that appears next,

88
00:08:29,207 --> 00:08:32,240
and then I'll see you in the following video.

