1
00:00:05,200 --> 00:00:09,280
Alright, so we've seen how to add and remove
items in a dictionary. Now we'll use those

2
00:00:09,280 --> 00:00:14,560
techniques in our menu program. We'll be
working in buy_computer_dict once more.

3
00:00:15,192 --> 00:00:19,760
What I'm going to do now is start by defining
an empty dictionary, just like we did with the

4
00:00:19,760 --> 00:00:25,096
empty list in the earlier version of this code.
Alright, so I'm going to come up here, and below

5
00:00:25,096 --> 00:00:31,479
the current_choice, line 9, we're
going to type computer_parts

6
00:00:31,479 --> 00:00:38,333
equals, and left and right curly braces. A
comment there - create an empty dictionary.

7
00:00:41,200 --> 00:00:44,800
Alright, so creating an empty dictionary
is very similar to creating an empty list,

8
00:00:44,800 --> 00:00:51,600
but this time we're using curly braces instead of
square brackets. I'll just add another space there.

9
00:00:51,600 --> 00:00:55,200
See it wants two spaces before the
inline comment. We'll just do a tab.

10
00:00:55,200 --> 00:01:00,195
So inside our loop, we'll check if the chosen part
is already in this new dictionary. If it is, 

11
00:01:00,195 --> 00:01:07,040
we'll remove it - otherwise, we'll add it. We're going
to go down here to under chosen_part,

12
00:01:07,040 --> 00:01:15,299
and basically, I'm adding some code here on line
15: if current_choice is in computer_paths colon,

13
00:01:16,250 --> 00:01:22,620
and a note there for us as a comment
- it's already in, so remove it.

14
00:01:24,160 --> 00:01:31,116
And we'll do a print parentheses, using an f-string,
so f then double quotes, Removing, then left and right

15
00:01:31,116 --> 00:01:36,055
curly braces, chosen_part in those
curly braces, and ending the line.

16
00:01:36,960 --> 00:01:41,486
Then the actual code would be
computer_parts.pop

17
00:01:41,486 --> 00:01:45,590
parentheses, and it will be
current_choice.

18
00:01:48,880 --> 00:01:55,440
Otherwise, we're going to add an else here now, and
the else - well we'll indent this code now because it's

19
00:01:55,440 --> 00:01:59,680
correctly saying that it's printing, it's, sorry,
it's adding the chosen part and that was the

20
00:01:59,680 --> 00:02:04,320
code that was there previously. We're back on that
shortly. But in terms of what we've added already,

21
00:02:04,320 --> 00:02:09,520
we've now added the code from line 15 onwards, to
remove an entry from the dictionary, and you can

22
00:02:09,520 --> 00:02:13,760
see that it's very similar to the code we used to
remove an item from the list - just that we're using

23
00:02:13,760 --> 00:02:19,660
pop instead of remove on line 18. Getting
back to adding a new entry to the dictionary,

24
00:02:19,660 --> 00:02:25,280
all we need to do is assign the value to the dictionary key,
and we'll leave our print in that we've got on line 20.

25
00:02:25,280 --> 00:02:29,280
And on line 21, I'm going to do
computer_parts

26
00:02:29,280 --> 00:02:33,251
left square bracket, and within
those brackets, current_choice

27
00:02:33,251 --> 00:02:36,960
is equal to, outside of the
square bracket, chosen_part.

28
00:02:39,360 --> 00:02:44,480
So line 21,  we use the key that's stored
in current_choice. Remember, 

29
00:02:44,480 --> 00:02:49,680
that will be one of the characters that's
a key in the available_parts dictionary.

30
00:02:49,680 --> 00:02:54,160
On line 13, if we go up there and have
a look, we check if the user's input

31
00:02:54,160 --> 00:02:59,200
is in available_parts.
By the time we get to line 21,

32
00:02:59,200 --> 00:03:02,960
we know that current_choice must be one
of the characters from 1 through 6.

33
00:03:03,760 --> 00:03:08,240
If you're a bit unsure about that, run the program
in the debugger, to see what line 21 is doing.

34
00:03:09,200 --> 00:03:14,720
Firstly, though, it'll be helpful to print out the
dictionary, so let's do that. That'll be outside

35
00:03:14,720 --> 00:03:20,240
of the else block, at the same level as our if
and else above. So what we're going to do there,

36
00:03:20,240 --> 00:03:29,760
is type print parentheses f double quotes
Your dictionary now contains, colon, then in 

37
00:03:29,760 --> 00:03:35,276
left and right curly braces, computer_parts.
Alright, so let's run this code now.

38
00:03:37,840 --> 00:03:43,317
So what I'm going to do is start. I've got the menu
printed. I'm going to choose a monitor, number 2.

39
00:03:44,880 --> 00:03:48,160
Move this back a little bit so we
can see - Your dictionary now contains

40
00:03:48,160 --> 00:03:54,160
2 monitor, which is correct. So that caused lines
20 and 21 to be executed - you can see there from

41
00:03:54,160 --> 00:03:59,760
the text. Specifically, we got the text adding
monitor, then on the next line, monitor was added

42
00:03:59,760 --> 00:04:05,587
to the dictionary with the key 2. Well let's
try again. This time, I'll add option 1, computer -

43
00:04:06,320 --> 00:04:12,080
Adding computer, Your dictionary now contains 2
monitor, and we've got our computer there as option 1, 

44
00:04:12,080 --> 00:04:16,959
as the key as well. So now we'll try removing something.
I'm going to enter 2 to remove the monitor -

45
00:04:16,959 --> 00:04:20,720
Removing monitor, Your dictionary now contains
only the computer, and you can see that is the

46
00:04:20,720 --> 00:04:25,920
case there, on screen. Remember that we
should always test boundary conditions.

47
00:04:25,920 --> 00:04:31,680
So here, one boundary condition would be removing
the last item to leave an empty dictionary.

48
00:04:31,680 --> 00:04:37,002
So let's enter 1 here now, to remove the computer, 
and that's going to leave the dictionary empty.

49
00:04:37,002 --> 00:04:41,360
And you can see that still works.
Experiment to make sure all the options work, 

50
00:04:41,360 --> 00:04:47,760
and that invalid options cause the menu to be printed
again. Enter zero to quit, when you've finished testing.

51
00:04:47,760 --> 00:04:53,360
Alright, so in the next few videos, we'll look at
linking dictionaries together. When dictionaries

52
00:04:53,360 --> 00:04:58,960
work together, or with lists, you can produce
some very useful code. See you in the next video.

