1
00:00:05,280 --> 00:00:10,080
Before we move on, I'll use the next few
videos to practice what we've learnt so far.

2
00:00:10,080 --> 00:00:13,760
So I'm going to start with another
menu program. In fact, we'll implement

3
00:00:13,760 --> 00:00:17,760
our buy_computer program from
the Improving our code lecture,

4
00:00:17,760 --> 00:00:23,227
in the Lists and Tuples section. And you can
see that I've put that into this project.

5
00:00:23,227 --> 00:00:29,360
Oh not another menu! Now look, note we're
not learning how to write menus here.

6
00:00:29,360 --> 00:00:33,280
We're looking at how to access and
add items to Python data structures.

7
00:00:33,840 --> 00:00:40,640
We've already seen how to do that with a list, now
we're doing the same thing with the dictionary.

8
00:00:40,640 --> 00:00:46,640
"But which one should I use?" Well, that's a question
new programmers often ask. There are so many options,

9
00:00:46,640 --> 00:00:52,640
and it can be confusing working out what you
should use, and when. We're going to write the same

10
00:00:52,640 --> 00:00:58,000
program using a dictionary instead of a list.
We can then talk about the advantages that

11
00:00:58,000 --> 00:01:03,040
a dictionary gives us, and the disadvantages.
When you understand the pros and cons of each,

12
00:01:03,040 --> 00:01:09,280
you'll be in a position to decide which one to use,
for your particular application. Understanding the

13
00:01:09,280 --> 00:01:14,480
different capabilities of these data structures
is the key here. Once you understand the different

14
00:01:14,480 --> 00:01:19,600
ways that a dictionary works, compared to a list,
then choosing between them becomes much easier.

15
00:01:20,960 --> 00:01:24,824
Alright, so you can see that I've got
the buy_computer.py program here. 

16
00:01:24,824 --> 00:01:29,680
Alright, so I'm going to start by creating a new Python file.
Let's go ahead and do that. We'll call this one

17
00:01:32,000 --> 00:01:34,320
buy_computer_dict.

18
00:01:39,280 --> 00:01:44,474
Now I'm going to paste in the dictionary
literal - the literal definition of our dictionary.

19
00:01:44,474 --> 00:01:47,600
What I'm going to do is grab
it from the old file - available_parts.

20
00:01:47,600 --> 00:01:53,840
Take a copy of that, then we'll make some
changes to it, so it's in the right format.

21
00:01:56,480 --> 00:02:00,720
Now obviously, that's a list. We're going to
change that, so it's a dictionary. So I'm going to

22
00:02:00,720 --> 00:02:08,833
change it to curly braces here, left and right.
Then we also need to add the key, so "1"

23
00:02:09,108 --> 00:02:15,920
in double quotes, colon and a space. We'll do the same
for the rest, and I'll just speed this part up.

24
00:02:19,760 --> 00:02:24,160
You saw there wasn't much editing needed.
The original list used square brackets,

25
00:02:24,160 --> 00:02:29,133
but I changed those to curly braces on lines
one through seven. Then I also added the keys

26
00:02:29,133 --> 00:02:33,520
at the start of each line. The keys, as
you saw, are all strings - the characters

27
00:02:33,520 --> 00:02:39,520
one to six. I added that in double quotes, and I
separated the keys and the values with a colon.

28
00:02:39,520 --> 00:02:44,960
Alright, so let's start there by displaying
the menu, and allowing the user to choose items.

29
00:02:44,960 --> 00:02:52,640
So to start, on line 9, by typing current_choice is equal to None,

30
00:02:52,640 --> 00:03:00,400
then on line 10, while current_choice is
not equal to zero in double quotes and a colon.

31
00:03:00,400 --> 00:03:04,559
Now in the earlier example, if we go
back to the original buy_computer,

32
00:03:04,559 --> 00:03:12,240
we initialize current choice, down here on line 13,
to a minus character. We have to give it a value,

33
00:03:12,240 --> 00:03:18,480
otherwise we'll get an error when we check the
value, down here on line 16. It doesn't matter what

34
00:03:18,480 --> 00:03:24,080
value we bind the variable to, as long as it's not
a valid choice. But of course, it also mustn't be zero

35
00:03:24,080 --> 00:03:31,647
otherwise we wouldn't enter the while loop. We use
the minus character here, but back in buy_computer_dict,

36
00:03:31,647 --> 00:03:36,320
on line 9 you can see that I've
initialized it to None. It doesn't really

37
00:03:36,320 --> 00:03:41,360
matter what you use. What's important is that we
want to initialize it to something, and we don't

38
00:03:41,360 --> 00:03:48,400
use a valid choice or zero. Alright, so next
we need to decide if the user's choice is valid.

39
00:03:48,400 --> 00:03:54,119
If it is, we print a message that we're adding
the part, otherwise we'll display the menu. 

40
00:03:54,119 --> 00:04:05,209
So let's start with the code for a valid choice. So on line
11, if current_choice is in available_parts colon,

41
00:04:05,209 --> 00:04:10,601
then it's chosen_part is equal to available_parts

42
00:04:11,122 --> 00:04:14,800
square bracket current_choice,
then closing square bracket.

43
00:04:15,600 --> 00:04:22,160
Then we'll print out a message: print
parentheses f, in double quotes, Adding, then

44
00:04:22,160 --> 00:04:27,840
left and right curly braces, chosen_part
in between those left and right curly braces,

45
00:04:28,640 --> 00:04:32,320
and then the end of the line. Alright, so
that's our if, and then I'm going to, at the

46
00:04:32,320 --> 00:04:37,680
same indentation level as the if but this time
on line 15. we'll type current_choice is

47
00:04:37,680 --> 00:04:43,120
equal to input, parentheses and in double quotes,
I'm going to put a greater than sign and a space,

48
00:04:43,120 --> 00:04:49,040
and then end the line. Right, so you can see that
I've used an f-string on line 13. Up until now

49
00:04:49,040 --> 00:04:56,021
we've been using dot format. f-strings are more
compact and now that Python 3.5 has reached end of life,

50
00:04:56,021 --> 00:05:02,160
all supported versions of Python can now use
f-strings. If you're still using Python 3.5, 

51
00:05:02,160 --> 00:05:09,157
then you'll need to change line 13 to use dot format
instead. Review the video on f-strings in section 3,

52
00:05:09,157 --> 00:05:14,240
if you're not sure how to use them. They're very
similar to placeholders with dot format, but you

53
00:05:14,240 --> 00:05:19,040
include the values inside the placeholder curly
braces, instead of passing them to the dot format

54
00:05:19,040 --> 00:05:23,767
method. And you can see that I've done that on line
13. Alright, so let's have a look at what we've done here.

55
00:05:23,767 --> 00:05:28,772
We're going to run the program, and we'll
see what it does before looking at how it does it.

56
00:05:29,760 --> 00:05:33,040
Now we haven't added the code to print the
menu yet, so you can see we just get an input

57
00:05:33,040 --> 00:05:40,842
prompt - the greater than. Our program is waiting
for us to make a choice, so I'm going to choose 2

58
00:05:40,842 --> 00:05:46,800
to add a monitor here, and you can see we've
got the message there - Adding monitor. So experiment

59
00:05:46,800 --> 00:05:51,360
with a few other options, and also enter something
invalid. So I'm going to add another option here:

60
00:05:51,360 --> 00:05:56,031
5 to add an hdmi cable. I'm going to do
7, which we know is an invalid option. 

61
00:05:56,031 --> 00:06:03,175
We get nothing added. We just get the prompt again. So how
does that work? Well, looking at the code - we've got

62
00:06:03,175 --> 00:06:08,947
a test there, on line 11. We're checking to see
if current_choice is in available_parts, 

63
00:06:08,947 --> 00:06:14,522
in other words, in the dictionary. available_parts
is obviously line one, we've defined.

64
00:06:14,880 --> 00:06:19,760
Now that's something that dictionaries allow you
to do, just like you can with lists and strings.

65
00:06:19,760 --> 00:06:25,217
There is a difference, though. When you use in
with a dictionary, you're only checking the keys.

66
00:06:25,440 --> 00:06:34,000
Now we can confirm that, in looking
at our list up there, by entering mouse.

67
00:06:34,000 --> 00:06:37,920
So mouse isn't a key in the dictionary, so
we get nothing added. We're back with an input

68
00:06:37,920 --> 00:06:44,660
prompt again. Now that might seem strange because
looking at line four, mouse is in the dictionary.

69
00:06:45,440 --> 00:06:48,720
Right, so on the left here,
we're using a dictionary. in

70
00:06:48,720 --> 00:06:55,520
is checking the keys, and mouse isn't a key in our
dictionary. So therefore line 9 is False. The key 4

71
00:06:55,520 --> 00:07:03,760
is in the dictionary so we get True on line 10.
On the right, we're working with a list. The value

72
00:07:03,760 --> 00:07:10,880
mouse is in the list, so we get True on line 9. The
value 4 isn't in the list and line 10 prints False.

73
00:07:12,960 --> 00:07:22,875
So remember that: when you use in with a list it checks the items
in a list. When you use in with a dictionary it checks the keys
in the dictionary not, the values.

74
00:07:22,875 --> 00:07:23,920
Alright, so back to our code,

75
00:07:23,920 --> 00:07:29,840
I'm going to enter 0 now to terminate the program,
and let's have a challenge to end the video.

76
00:07:32,000 --> 00:07:37,200
So you want to add an else block to our code, and
print the menu when the user's choice isn't in

77
00:07:37,200 --> 00:07:42,800
the dictionary. Now if you're not sure how to do
that, review the video Iterating over a dictionary.

78
00:07:42,800 --> 00:07:45,920
The loop in your else block will
be very similar to the code we used

79
00:07:45,920 --> 00:07:48,240
to iterate over the dictionary's
keys and values.

80
00:07:51,760 --> 00:07:58,240
Alright, so that's the challenge, and I'll go
over my solution to it in the next video.

