1
00:00:04,400 --> 00:00:11,930
Alright, so let's now look at
improving the code in our buy_computer program.
So you can see I've got buy_computer.py

2
00:00:11,930 --> 00:00:16,460
on-screen, and we want to see how we can
use a list to tidy up those conditions -

3
00:00:16,460 --> 00:00:21,040
these are the conditions on lines 15
through 26, inclusive. Now at the moment,

4
00:00:21,050 --> 00:00:25,489
our program isn't working because we
haven't included 7 in the string, on

5
00:00:25,489 --> 00:00:32,000
line 13. Rather than hard coding the
values 1, 2, 3, 4, 5, 6, what we can do is get

6
00:00:32,000 --> 00:00:37,370
Python to generate it from the number of
items in the list. Ideally, we'd use a

7
00:00:37,370 --> 00:00:41,929
list comprehension for that. Now we'll be
covering comprehensions and generators

8
00:00:41,929 --> 00:00:46,339
later in the course, but I'm going to put
the code in now. I'm doing that because I

9
00:00:46,339 --> 00:00:50,539
want to show you good practices, when
programming in Python. So our code is

10
00:00:50,539 --> 00:00:53,600
going to use a for loop, but I want you
to be aware that there are more

11
00:00:53,600 --> 00:00:58,080
efficient ways of doing some things. So
let's see this comprehension.

12
00:00:58,080 --> 00:01:02,820
So directly below line 8, I'm going
to type valid_choices

13
00:01:02,820 --> 00:01:11,040
equals left square bracket str
parentheses, with i in those parentheses.

14
00:01:11,040 --> 00:01:19,070
Then for i in range left parenthesis 1
comma len, and in parentheses, available

15
00:01:19,070 --> 00:01:24,020
underscore parts. Then we've got our
right parenthesis plus 1, then our

16
00:01:24,020 --> 00:01:28,760
closing right parenthesis and then our
closing right square bracket. Then we're

17
00:01:28,760 --> 00:01:34,490
going to print valid choices. So I'm
printing out the valid choices list on

18
00:01:34,490 --> 00:01:42,960
line 10, just so you can see what's being
produced. So let's run the program,

19
00:01:42,960 --> 00:01:48,500
and we can see that valid_choices is now a list containing the strings 1 to 7 -

20
00:01:48,500 --> 00:01:52,060
you can see that right at the top of
the output pane. Alright, so looking at

21
00:01:52,070 --> 00:01:56,990
that code on line 9, it looks horrible.
When you have worked through the

22
00:01:56,990 --> 00:02:00,100
lectures on comprehensions, though, it will
make perfect sense.

23
00:02:00,100 --> 00:02:04,910
Until then, be aware that there are more
efficient ways of doing things, but that

24
00:02:04,910 --> 00:02:08,508
you can't learn everything at once.
Alright, so what I'm going to do is

25
00:02:08,508 --> 00:02:14,260
comment out that line and replace it
with some code that'll make sense.

26
00:02:21,240 --> 00:02:27,380
So I'm going to type valid_choices is equal to an empty

27
00:02:27,380 --> 00:02:32,840
list and left and right square brackets.
Then on the next line I'm going to type for i

28
00:02:32,840 --> 00:02:40,220
in range parentheses 1 comma len
parentheses, available_parts

29
00:02:40,230 --> 00:02:44,760
in that parentheses, closing right
parenthesis plus 1, then our closing

30
00:02:44,760 --> 00:02:52,680
right parenthesis and a colon. On the
next line, valid_choices.append,

31
00:02:52,680 --> 00:02:58,240
and in parentheses, str, parentheses i and the two closing right parentheses.

32
00:02:58,240 --> 00:03:03,180
And we'll leave the print
valid choices as it is on line 13. Now be

33
00:03:03,180 --> 00:03:07,830
careful with the parentheses on line 11.
We add 1 to the length of available

34
00:03:07,830 --> 00:03:12,480
underscore parts, because the final
value in range isn't included. The 1

35
00:03:12,480 --> 00:03:16,230
isn't inside the len function's arguments -
we're adding 1 to the length and

36
00:03:16,230 --> 00:03:21,630
that's obviously outside of the
parentheses there. You can see we're

37
00:03:21,630 --> 00:03:24,120
missing white space around operator
there, so I need to put a space there,

38
00:03:24,120 --> 00:03:32,730
just to keep IntelliJ happy. Alright, so
we run the program again now, stop and

39
00:03:32,730 --> 00:03:41,780
re-run there, and you can see we've still
got the same output, 1, 2, 3, 4, 5, 6, 7 as a list.

40
00:03:41,780 --> 00:03:46,560
So we're using this str function, on
line 12, because the customer is entering

41
00:03:46,560 --> 00:03:51,330
strings, and remember that input will return
a string. If their input is going to

42
00:03:51,330 --> 00:03:54,989
match an item in this list, then this
list should also contain strings. Ok now

43
00:03:54,989 --> 00:03:58,769
that we have a list that automatically
contains the options allowed, we can use

44
00:03:58,769 --> 00:04:04,140
that list on line 18. Let's go ahead and
do that, so instead of putting if current

45
00:04:04,140 --> 00:04:09,900
choice is in 1, 2, 3, 4, 5, 6, we'll just
need to replace that, to be valid

46
00:04:09,900 --> 00:04:14,880
underscore choices. Alright, so far so
good. The last step now, is to replace all

47
00:04:14,880 --> 00:04:19,589
those if elif statements with simpler
code. We have a list of the available

48
00:04:19,589 --> 00:04:23,520
parts, and we know which part the user
chose, so it's quite easy to get the

49
00:04:23,520 --> 00:04:27,780
part name from the list. We can index
into available_parts to get

50
00:04:27,780 --> 00:04:31,940
the required item.
There are two slight complications:

51
00:04:31,940 --> 00:04:36,140
First, the choice is one greater than the items
index. That's easy to fix - we just

52
00:04:36,150 --> 00:04:39,690
subtract one from it.
The second complication is that current

53
00:04:39,690 --> 00:04:44,850
underscore choice is a string, and indexes
have to be ints. We saw how to handle

54
00:04:44,850 --> 00:04:49,050
that when we used the int function
earlier. So let's have a look at changing

55
00:04:49,050 --> 00:04:54,330
this code. So directly below the print,
I'm going to delete the other if

56
00:04:54,330 --> 00:05:01,800
code shortly. We're going to put index
equals int parentheses current choice -

57
00:05:01,800 --> 00:05:08,300
again we're using minus one there -
chosen_parts is equal to

58
00:05:08,300 --> 00:05:14,920
available_parts, and in
square brackets, index. Then the third line,

59
00:05:14,920 --> 00:05:21,200
computer_parts.append, and in
parentheses, chosen_part. Let's delete the

60
00:05:21,200 --> 00:05:28,280
rest of this if and elif block, or
statements, just to tidy things up.

61
00:05:28,280 --> 00:05:32,280
I think you'd agree, that looks a lot tidier.
We've effectively replaced twelve lines

62
00:05:32,280 --> 00:05:36,660
of code with three lines. We've also
allowed the program to be updated, just

63
00:05:36,660 --> 00:05:40,500
by adding or removing items from the
available_parts list, up there

64
00:05:40,500 --> 00:05:44,669
on lines 1 through 7. So let's
check that the program works before

65
00:05:44,669 --> 00:05:48,800
changing the list. Go ahead and do that.

66
00:05:49,060 --> 00:05:57,680
So I'm gonna start by adding a computer,
option 1, a dvd drive, option 7.

67
00:05:57,680 --> 00:06:01,620
I'm gonna try to add option 8 - that should display the menu again and it did do that,

68
00:06:01,620 --> 00:06:04,979
so that's working fine, and if you type
anything else that's not a valid option,

69
00:06:04,980 --> 00:06:10,600
the menu will also be displayed again.
Let's test that. I'll enter my name here, Tim,

70
00:06:10,600 --> 00:06:14,120
same deal - we get the menu back again.
And that's a very strange thing for a

71
00:06:14,130 --> 00:06:18,060
customer to choose, but we've got no
control over what people may type into

72
00:06:18,060 --> 00:06:21,060
our programs. You can see we've got the
menu displayed again, so that's good -

73
00:06:21,060 --> 00:06:24,660
our code behaves sensibly and doesn't
crash, it doesn't crash in that scenario.

74
00:06:24,660 --> 00:06:29,940
Alright, so let's finish by entering
zero, and we get our final list which

75
00:06:29,940 --> 00:06:34,100
contains a computer and a dvd drive. So
far so good.

76
00:06:34,100 --> 00:06:40,900
Let's go back up and what we'll do, is
we'll delete the mouse mat from our list.

77
00:06:40,900 --> 00:06:47,230
Go ahead and do that. Run the program
again, and you can see it still works but

78
00:06:47,230 --> 00:06:50,800
now it's minus the mouse mat, because
we've deleted it from our available

79
00:06:50,800 --> 00:06:54,340
underscore parts list. Alright, you can
see that things are still working.

80
00:06:54,340 --> 00:06:57,520
The menu now only contains six items and we
can add them to the list of parts we

81
00:06:57,520 --> 00:07:04,570
want to buy. Let's try a few; computer
then hdmi cable, option 7 - we now go

82
00:07:04,570 --> 00:07:08,410
back to the menu, whereas previously, of
course, we had option 7 as being the

83
00:07:08,410 --> 00:07:14,160
dvd drive - zero to exit and the two items
I chose were computer and hdmi cable.

84
00:07:14,160 --> 00:07:18,100
Everything's working as it should do. So
we've used a few lists in this program.

85
00:07:18,100 --> 00:07:22,440
On lines 1 to 7, we've got a list of
parts that are available to buy. On lines

86
00:07:22,440 --> 00:07:27,080
9 to 11, we create an empty list
and then add the valid choices to it.

87
00:07:27,080 --> 00:07:31,620
Line 14 defines another empty
list, computer_parts, which

88
00:07:31,620 --> 00:07:38,540
will contain the parts that the customer wants to buy, and we're appending their items -

89
00:07:38,540 --> 00:07:42,310
ones that they've chosen to
buy - on line 21, so that ultimately

90
00:07:42,310 --> 00:07:46,479
contains the list of items that they
want to purchase. So using lists for the

91
00:07:46,479 --> 00:07:51,310
menu items makes the code much shorter.
More importantly, though, it's much easier

92
00:07:51,310 --> 00:07:55,320
to maintain. We can add and remove items from the available_parts list

93
00:07:55,320 --> 00:07:58,660
and we don't have to change any
other code, as a result of doing that,

94
00:07:58,660 --> 00:08:02,349
as you saw. So test the program thoroughly.
You might want to add things like a

95
00:08:02,349 --> 00:08:06,789
webcam, microphone and loudspeakers to
the list. Also, make sure it copes with

96
00:08:06,789 --> 00:08:10,690
invalid choices. When testing a program
like this, don't just focus on the

97
00:08:10,690 --> 00:08:15,380
correct input - make sure you try to think
of other things that a user might type.

98
00:08:15,380 --> 00:08:18,220
Alright, so in the next video, moving
ahead, we're gonna get the program to

99
00:08:18,220 --> 00:08:23,700
cope if the customer changes their mind.
See you in the next video.

