1
00:00:04,750 --> 00:00:09,639
Alright, so let's look at appending items to a list. For this example, we'll allow

2
00:00:09,639 --> 00:00:14,020
customers to choose which parts they
need when buying a computer. I'll keep it

3
00:00:14,020 --> 00:00:18,130
simple to start with - we'll just offer
five items for sale. Now we saw how

4
00:00:18,130 --> 00:00:21,400
to write a simple menu, in the challenge
at the end of the last section.

5
00:00:21,400 --> 00:00:25,540
This program will start off very similar
to that. So let's start by creating a new

6
00:00:25,540 --> 00:00:40,240
Python file. I'm going to call it buy
underscore computer.py.

7
00:00:40,240 --> 00:00:45,280
Alright, so first we need to initialize a couple of
variables. We need a variable to store

8
00:00:45,280 --> 00:00:50,499
the customers choice, as well as an empty
list to add items to. So on line one,

9
00:00:50,499 --> 00:00:55,149
current_choice = double
quotes, with a dash character or minus

10
00:00:55,149 --> 00:00:59,460
character in there. We talked about the
reasons for using that in the last section.

11
00:00:59,460 --> 00:01:01,940
Line two, computer underscore parts

12
00:01:01,940 --> 00:01:10,940
equals, and just left and right square
braces, and a comment there, create an empty list.

13
00:01:10,940 --> 00:01:15,850
Again, that's going to be list to
store the items. Alright, so the code to

14
00:01:15,850 --> 00:01:19,690
display the menu is the same as we've
used before. It's just the description of

15
00:01:19,690 --> 00:01:25,450
the items that have changed. So I'm going to start on line four - I'm typing while current

16
00:01:25,450 --> 00:01:33,520
underscore choice is not equal to, in 
single quotes, 0 colon. Then on the next line,

17
00:01:33,520 --> 00:01:43,710
if current underscore choice is in and
in double quotes, 1 2 3 4 5 colon print

18
00:01:43,710 --> 00:01:50,580
parentheses double quotes, adding
left and right curly braces double quotes,

19
00:01:50,580 --> 00:01:55,570
dot format, and in parentheses, current
underscore choice and two right to

20
00:01:55,570 --> 00:02:00,250
parentheses to finish the line. Press
enter there, and backspace. We're gonna start on

21
00:02:00,250 --> 00:02:05,260
line 8, then else : then we're going to
print out our menu option, so print

22
00:02:05,260 --> 00:02:10,620
parentheses, Please add options from the list below,

23
00:02:11,280 --> 00:02:17,860
and double quote and right parenthesis.
Next line, print parentheses double

24
00:02:17,860 --> 00:02:24,760
quotes 1 : computer, double quotes and
right parenthesis. Next line, print

25
00:02:24,760 --> 00:02:32,080
parentheses 2, and double quotes 2, rather,
monitor. That's the next item there.

26
00:02:32,080 --> 00:02:39,040
Moving on to the next line, print
parentheses double quotes 3 : keyboard,

27
00:02:39,040 --> 00:02:43,180
and our double quotes and right parenthesis.
Next line, print parentheses 4 double

28
00:02:43,190 --> 00:02:49,609
quotes, in 4 rather, or four in double
quotes, mouse, and double quotes and

29
00:02:49,609 --> 00:02:54,409
parenthesis. Next line, print 5 in
parentheses, sorry in double quotes,

30
00:02:54,409 --> 00:03:00,379
which is also in parentheses, mouse mat.
There's our 5 options, and the only other

31
00:03:00,379 --> 00:03:04,129
one we want after entering the double
quote and right parenthesis is our

32
00:03:04,129 --> 00:03:09,280
option to exit, print. That's on the next
line, parentheses double quotes 0 colon

33
00:03:09,280 --> 00:03:14,780
to finish. Double quote and right
parenthesis, and then we're going to go

34
00:03:14,780 --> 00:03:19,400
down to line 17 at the same level as the
if and the else, current underscore

35
00:03:19,400 --> 00:03:25,549
choice is equal to input, left and right
parentheses. When you're starting out

36
00:03:25,549 --> 00:03:29,629
learning to program, it's a good idea to
run your code often. That way you can

37
00:03:29,629 --> 00:03:33,019
spot errors and fix them, without having
hundreds of lines of code to work

38
00:03:33,019 --> 00:03:36,410
through. I'm going to run the program
that we've got so far, to make sure it

39
00:03:36,410 --> 00:03:42,560
works as we expect. Let's run it. You can
see there we've got a menu, so that's a

40
00:03:42,560 --> 00:03:46,220
good start. Let's just start by trying to add
a few options. We're going to start by adding a

41
00:03:46,220 --> 00:03:52,000
monitor, option 2, and we got the
message there, Adding 2 printed.

42
00:03:52,000 --> 00:03:58,780
Try adding 4 now, for mouse, Adding 4. So
mouse also works. Now you should test all

43
00:03:58,790 --> 00:04:02,540
the valid options to make sure you get
the appropriate message printed. I won't

44
00:04:02,540 --> 00:04:05,120
go through all of them in the video.
Pause the video and check the other

45
00:04:05,120 --> 00:04:09,760
options yourself. Now we should also test
what happens when we enter something invalid,

46
00:04:09,760 --> 00:04:15,420
so I'm gonna try entering 8 here,
which obviously, isn't an option there.

47
00:04:15,420 --> 00:04:18,680
The menu doesn't have an option 8 so
nothing's added. Our code displays the

48
00:04:18,680 --> 00:04:23,419
menu again, so that's also working fine.
We'll try 0 now to finish, and the

49
00:04:23,419 --> 00:04:27,950
programs exited.
Alright, so far so good. The next step

50
00:04:27,950 --> 00:04:32,720
is to add the items to the list. At the
moment, our computer_parts list -

51
00:04:32,720 --> 00:04:36,680
that's the one we've defined up here on
line two, is empty.

52
00:04:36,680 --> 00:04:40,040
We've used square brackets with nothing
between them on line two, and that

53
00:04:40,040 --> 00:04:44,750
creates an empty list, as the comment
said. Inside the if block, we can append

54
00:04:44,750 --> 00:04:49,550
new items to the list. So let's go ahead
and do that. We're going to add it under

55
00:04:49,550 --> 00:04:53,330
the print statement on line six. so I'm
going to type if, and indent it at the

56
00:04:53,330 --> 00:04:59,480
same level, current_choice is
equal to 1, and 1 in single quotes -

57
00:04:59,480 --> 00:05:04,440
two equal signs there - colon. Then we're
going to do computer_ parts

58
00:05:04,440 --> 00:05:11,940
dot append, and in parentheses,
computer in double quotes. So the code

59
00:05:11,940 --> 00:05:16,400
here, if the customer chooses option 1,
we add a computer to the list. We do that

60
00:05:16,400 --> 00:05:21,040
by calling the list's append method. The
argument is the thing we want to append,

61
00:05:21,040 --> 00:05:26,510
a computer in this case. That's the text
in double quotes on line eight. Alright,

62
00:05:26,510 --> 00:05:29,930
so the other items are more of the same.
I'm going to paste the code in here to

63
00:05:29,930 --> 00:05:35,520
save a bit of time. Alright, so there's
the code that I've pasted in there now.

64
00:05:35,520 --> 00:05:38,260
Alright, so pause the video while you
enter the remaining code and come back

65
00:05:38,270 --> 00:05:43,850
when you're ready to continue. Okay, so
those blocks of code use the append

66
00:05:43,850 --> 00:05:49,040
method to add the items to the list. So
there's a few things wrong with this code.

67
00:05:49,040 --> 00:05:52,160
We have to duplicate the five
strings, for one thing. The string

68
00:05:52,160 --> 00:05:58,610
computer, on line eight, also appears in
the code on line 20. Let's take the

69
00:05:58,610 --> 00:06:02,220
opportunity to fix this up as well. I'm
going to remove that extra line there.

70
00:06:02,220 --> 00:06:08,180
So you can see the duplication there, again, computer line eight, as well as on line 19 now.

71
00:06:08,180 --> 00:06:11,330
Adding a new option also
involves a lot of work. We've had to

72
00:06:11,330 --> 00:06:15,770
change the code in three places. But
let's make sure it works before we start

73
00:06:15,770 --> 00:06:18,980
improving the code. To make sure it's
working, we need to print out the

74
00:06:18,980 --> 00:06:22,820
computer_parts list that we're
appending to. So let's do that right at

75
00:06:22,820 --> 00:06:29,360
the end of the program. So I'm going to
do that right down here, on line 28,

76
00:06:29,360 --> 00:06:33,350
indented to the left so it's executed
when the program finishes, print

77
00:06:33,350 --> 00:06:39,020
parentheses computer_parts, and closing right parenthesis there.

78
00:06:39,020 --> 00:06:42,080
And again, make sure that line
isn't indented. Basically, we want the

79
00:06:42,080 --> 00:06:46,840
list to be printed out when the loop
exits, as I mentioned. So let's run that.

80
00:06:46,840 --> 00:06:52,420
Let's choose some items now, to add to
our list. We'll start with option 1, computer.

81
00:06:52,420 --> 00:06:56,720
Let's do another one,
option 4, which is the mouse.

82
00:06:56,720 --> 00:07:02,480
Let's exit by selecting zero. As you can see
we've got a list there containing a

83
00:07:02,480 --> 00:07:08,980
computer and a mouse. So that code's
working fine, but it isn't very clean, though.

84
00:07:08,980 --> 00:07:12,560
There's a lot of duplication, and
making changes to the menu is a lot

85
00:07:12,560 --> 00:07:16,520
harder than it should be. Improving the
program is a good way to see some of the

86
00:07:16,520 --> 00:07:19,700
other things we can do with lists, and
we'll make a start on that in the next

87
00:07:19,700 --> 00:07:24,560
video. But I'm going to finish this video
first with a mini challenge:

88
00:07:24,560 --> 00:07:30,800
Change the program to have another
option, option 6, for a HDMI cable.

89
00:07:30,800 --> 00:07:34,080
So remember that you'll need to make a
change in three places. Have a go,

90
00:07:34,080 --> 00:07:40,080
test your code thoroughly and I'll go over my
solution to that in the next video.

