1
00:00:04,180 --> 00:00:07,750
Alright, so let's look at improving the
code that we've been working on, in the

2
00:00:07,750 --> 00:00:14,320
last few videos. Looking at our menu
first, on lines 21 through 27. So our menu

3
00:00:14,320 --> 00:00:18,460
options can, effectively, be stored as a
list, and what we can do is then iterate

4
00:00:18,460 --> 00:00:23,529
over the list, using a for loop to print
out the options. So to do that, what we

5
00:00:23,529 --> 00:00:26,109
need to define a list and we'll
do that right at the top of the program,

6
00:00:26,109 --> 00:00:34,000
on line one. Alright, so I'm gonna start by
typing available_parts is

7
00:00:34,000 --> 00:00:40,680
equal to left square bracket double
quotes computer double quote comma.

8
00:00:40,680 --> 00:00:49,620
Next line, monitor in double quotes comma,
next line, double quotes keyboard double

9
00:00:49,630 --> 00:00:56,590
quote, next line mouse in double quotes
comma. Next line, mouse mat in

10
00:00:56,590 --> 00:01:04,479
double quotes comma and then finally our hdmi cable in double quotes. And I'm gonna press

11
00:01:04,479 --> 00:01:10,450
ENTER to leave the square bracket, on
a line by itself, on line 7. Now that

12
00:01:10,450 --> 00:01:17,340
we've done that, we can come down and
have a look at our code on lines 28 through 34,

13
00:01:17,340 --> 00:01:20,320
and we can replace that with a for loop.
So I'm gonna start by deleting that code

14
00:01:20,320 --> 00:01:24,580
now. Then I'm going to tab over to the
same indentation level and I'm going to

15
00:01:24,580 --> 00:01:31,690
type for_part in available_parts colon, print and in

16
00:01:31,690 --> 00:01:38,290
parentheses, part.
Alright, so let's run the program.

17
00:01:38,290 --> 00:01:41,260
It's still working, as you can see, so we're
part of the way there. We've got the

18
00:01:41,260 --> 00:01:45,190
parts printed out but we haven't got the
number. The customer's no way of knowing

19
00:01:45,190 --> 00:01:49,840
which numbers to type in. Now we
could get the index of each part and add

20
00:01:49,840 --> 00:01:56,920
one to it. So what I mean is, we'll close down this program.

21
00:01:56,920 --> 00:02:00,580
So again, what I'm saying we
could do here is, change the print, print

22
00:02:00,580 --> 00:02:06,119
parentheses double quotes left and right
curly braces 0, then right clearly brace

23
00:02:06,119 --> 00:02:10,090
colon, and the same for 1 - left and
right curly braces with a 1 in the

24
00:02:10,090 --> 00:02:14,920
middle there, double quote dot format. And
then in parentheses, available underscore

25
00:02:14,920 --> 00:02:21,860
parts dot index,
and in parentheses, after the index part

26
00:02:21,860 --> 00:02:27,350
and outside of the right parentheses,
plus 1 comma space part, and then two

27
00:02:27,350 --> 00:02:32,450
right parentheses to finish the line. So
we could do that. What I've done there is,

28
00:02:32,450 --> 00:02:35,750
I've used string formatting and
replacement fields there, to display the

29
00:02:35,750 --> 00:02:39,740
index number and the part, and I'm adding
one to the index, and that's because

30
00:02:39,740 --> 00:02:43,910
indexes start at zero. We want the first
number to be number one on the screen,

31
00:02:43,910 --> 00:02:49,670
rather than number zero. So let's run the
program to see that it works, and you can

32
00:02:49,670 --> 00:02:53,780
see that the menu's appearing now, with
numbers. Now you may get a dialog saying

33
00:02:53,780 --> 00:02:57,230
that buy.computer's not allowed to run in
parallel, if you didn't exit when you

34
00:02:57,230 --> 00:03:01,160
last ran. You saw that I closed down the
program. And again, you've seen it a few

35
00:03:01,160 --> 00:03:04,400
times - just click stop and rerun to run
the latest version, if you get that

36
00:03:04,400 --> 00:03:08,570
dialog. But getting back to our code - that
looks good, and if we add new items to a

37
00:03:08,570 --> 00:03:14,330
list, they should automatically appear in
the menu. Let's try that. I'll exit out of

38
00:03:14,330 --> 00:03:22,880
the program. So let's add another item
here. We'll add a dvd drive - dvd drive and

39
00:03:22,880 --> 00:03:28,900
I need to add a comma on the
previous line, and now if I run the program,

40
00:03:28,900 --> 00:03:30,620
I've only changed it in one place. Notice

41
00:03:30,620 --> 00:03:36,580
that the dvd drive is appearing as
option 7. Now looking at the code,

42
00:03:38,590 --> 00:03:44,270
we still need to change this code - the code
on lines 15 through 26 - and we're going

43
00:03:44,270 --> 00:03:47,740
to do that soon. Before we do that, though,
I want to show you a better way

44
00:03:47,740 --> 00:03:53,360
of writing lines 29 and 30. What we've done
there isn't very efficient, and that's

45
00:03:53,360 --> 00:03:57,530
because Python has to look up each item
in the list, in order to get its index

46
00:03:57,530 --> 00:04:01,550
position. And that's not very efficient,
especially if you start dealing with

47
00:04:01,550 --> 00:04:06,140
huge lists. Index can be useful, but if
you see it being used in a for loop

48
00:04:06,140 --> 00:04:10,010
like we're doing, then there's a much
more efficient way, and we'll see what

49
00:04:10,010 --> 00:04:13,390
that is in the next video.

