1
00:00:04,670 --> 00:00:08,840
Alright, so it's time to write the code
that lets our user select songs from the

2
00:00:08,840 --> 00:00:13,129
albums lists. You'll have noticed, in the
demonstration, that the program keeps

3
00:00:13,129 --> 00:00:17,380
displaying the menus until an invalid
choice is made, now that's a good use for

4
00:00:17,380 --> 00:00:21,880
a while loop. We used a while look at the
end of the previous section, when we

5
00:00:21,880 --> 00:00:26,779
created a simple menu program. We then
extended that, to offer our menu of parts

6
00:00:26,779 --> 00:00:30,109
for a computer. You might want to review
those earlier lectures, before continuing

7
00:00:30,109 --> 00:00:34,430
with this one. You'll notice a lot of
similarity between the earlier menu

8
00:00:34,430 --> 00:00:39,079
program, and the code we're about to
write. Alright, so I'm going to delete the

9
00:00:39,080 --> 00:00:46,240
line that printed the albums. Instead
what we're going to do, is type in while

10
00:00:46,240 --> 00:00:50,940
True, with a capital T there and colon.
Next we'll print some brief

11
00:00:50,940 --> 00:00:55,360
instructions and the titles of the
albums. So I'm going to print, on line 4,

12
00:00:55,370 --> 00:01:03,949
parentheses double quotes Please choose
your album, and in parentheses, Invalid

13
00:01:03,949 --> 00:01:09,890
choice exits, right parenthesis colon.
Then we've got our double quote and

14
00:01:09,890 --> 00:01:15,120
right parenthesis there. Let's put the
titles for the albums now, so it's for

15
00:01:15,120 --> 00:01:22,560
index comma. Then in parentheses I'm
going to type title comma artist comma

16
00:01:22,560 --> 00:01:29,300
year and comma songs, then we've got our
right parentheses there, in enumerate,

17
00:01:29,300 --> 00:01:35,659
then in parentheses, albums, then right
parenthesis and then the colon. Then on the

18
00:01:35,660 --> 00:01:38,960
next line, we're going to print out the
title, artist, year and song, so it's print

19
00:01:38,960 --> 00:01:44,420
parentheses double quotes, left and right 
curly braces for each, so basically

20
00:01:44,420 --> 00:01:53,780
duplicating that four times, one, two, three
four- so a total of five curly braces there -

21
00:01:53,780 --> 00:01:57,280
then closing double quote. Then on the
next line, just for formatting reasons,

22
00:01:57,289 --> 00:02:00,979
I'm going to type .format - you can do that
on one line if you prefer. So it's dot

23
00:02:00,980 --> 00:02:10,460
format parentheses, index + 1 comma,
then we want our title comma artist comma

24
00:02:10,460 --> 00:02:17,140
year comma and songs comma. Then we've got
our two right parentheses.

25
00:02:17,140 --> 00:02:21,299
So we're using a for loop to iterate
over the albums. We also want the index

26
00:02:21,299 --> 00:02:24,420
numbers and we've seen, previously, how to
get them by using the enumerate

27
00:02:24,420 --> 00:02:29,790
function. I've split line 6 and 7
into two lines, just to show it all on

28
00:02:29,790 --> 00:02:34,110
video. Again, you can put that on one line
if you prefer. So this code is very

29
00:02:34,110 --> 00:02:38,540
similar to the code we've seen before.
Where it's different, though, is the parentheses

30
00:02:38,540 --> 00:02:44,740
around title, artist, year and songs on
line 5. I introduced nested data

31
00:02:44,740 --> 00:02:48,120
structures by mentioning those
parentheses. That was at the end of the

32
00:02:48,120 --> 00:02:52,110
More Unpacking video, earlier in this
section. Once we've see why they're

33
00:02:52,110 --> 00:02:55,680
necessary, it makes perfect sense, and
when you've seen the error you get when

34
00:02:55,680 --> 00:02:58,460
you forget them, you'll
know that you need to use them.

35
00:02:58,460 --> 00:03:01,620
So let's start with the error. Our while
loop's gonna go round and round forever,

36
00:03:01,620 --> 00:03:05,790
which isn't great, so I'm going to add a
break to prevent that. So I'm going to

37
00:03:05,790 --> 00:03:11,420
put the break down here, on line 9,
indented to the same level as the for loop.

38
00:03:11,420 --> 00:03:18,060
Alright, so let's just run this to confirm
that it does work. Run the code.

39
00:03:18,060 --> 00:03:21,920
Alright, so that's working okay, as you can see.
What happens if I remove those

40
00:03:21,930 --> 00:03:26,220
parentheses? Let's go ahead and do that.
So I'm going to change this up here, to remove

41
00:03:26,220 --> 00:03:34,680
the parentheses, and you can see there's
no errors showing, but if I run this now

42
00:03:34,680 --> 00:03:38,160
we actually get an error:
not enough values to unpack, expected

43
00:03:38,160 --> 00:03:43,200
5, got 2. So we asked Python to
unpack five values but enumerate only

44
00:03:43,200 --> 00:03:47,340
returns two. When you get an error like
that, it's worth checking what

45
00:03:47,340 --> 00:03:51,630
enumerate is returning. So comment out
this loop, which I'll do now, the loop

46
00:03:51,630 --> 00:03:58,120
that we've been using. We'll actually 
comment out those three lines.

47
00:03:58,120 --> 00:04:02,160
Instead, what I'm going to do, is
come down here onto line 9 and type

48
00:04:02,160 --> 00:04:07,760
for space index comma value in enumerate and in

49
00:04:07,769 --> 00:04:13,139
parentheses, albums, right parentheses and
: We're going to print, in parentheses,

50
00:04:13,140 --> 00:04:21,959
index comma value. Then I'll leave line
12 as it was. Run the program now,

51
00:04:21,959 --> 00:04:26,480
and we've got four tuples printed out. Their index
positions are printed first,

52
00:04:26,480 --> 00:04:30,440
and then each tuple. If enumerate only returns two items,

53
00:04:30,440 --> 00:04:34,900
then we can only unpack two items in the
for loop. So examine this output

54
00:04:34,900 --> 00:04:40,150
carefully, to check what those two items
represent. The first is an int, the index

55
00:04:40,150 --> 00:04:44,480
position of each item, but the second
item, as you can see there, is a tuple,

56
00:04:44,480 --> 00:04:50,220
and it actually contains four items; the
title, the artist, the year, as well as the

57
00:04:50,220 --> 00:04:55,800
list of songs. If it's a  tuple, we should
also be able to unpack it, and we can,

58
00:04:55,800 --> 00:05:00,820
and once again, I'll take this slow. Let's
start by unpacking value. So I'm gonna close

59
00:05:00,820 --> 00:05:05,470
down the run window. We'll go back to our
for loop here. On line 10, I'm going to

60
00:05:05,470 --> 00:05:14,740
insert title comma artist comma year
comma songs equals value. Instead of

61
00:05:14,740 --> 00:05:18,080
value, we're actually going to print out
those four values. So we'll leave index in,

62
00:05:18,080 --> 00:05:27,360
and we'll print index, title, artist, year and songs. Run the program, and let's check out the output.

63
00:05:27,360 --> 00:05:32,620
You can see there, that line
10 is unpacking the value tuple into

64
00:05:32,620 --> 00:05:37,200
our four variables, and in the output now,
we get those four variables printed out,

65
00:05:37,200 --> 00:05:44,320
each time around the loop. You can see that over
to the right-hand side. Now that's not

66
00:05:44,320 --> 00:05:48,220
surprising - we've done things like that a
few times now. Where it gets interesting,

67
00:05:48,220 --> 00:05:52,990
and why those parentheses were
necessary, is when we combine lines 9

68
00:05:52,990 --> 00:05:58,630
and 10. So compare those two lines -
close down the run window. So compare

69
00:05:58,630 --> 00:06:03,500
lines 9 and 10, to line 5, which is
currently commented out. On line 5,

70
00:06:03,500 --> 00:06:08,780
we want to unpack a single tuple into four
variables, just like we're doing on line 10.

71
00:06:08,780 --> 00:06:13,680
But because that tuple's one of the
pair of values, we need to enclose title,

72
00:06:13,690 --> 00:06:23,950
artist, year end songs in parentheses. So
let's do that. We're now doing all the

73
00:06:23,950 --> 00:06:28,840
unpacking on one line, instead of doing
it in two steps on lines 9 and 10.

74
00:06:28,840 --> 00:06:38,140
I'm gonna un-comment that block now, and
let's rerun the program.

75
00:06:38,140 --> 00:06:42,840
You can see that that works and we get a very similar output now, from both loops. The index numbers are

76
00:06:42,850 --> 00:06:47,500
different - that's because we added one to
the index on line 7 - but we're

77
00:06:47,500 --> 00:06:51,880
getting the same values otherwise. And
we'll do something similar again, when we

78
00:06:51,880 --> 00:06:55,780
come to display the list of songs for an
album. Now that'll be in the video

79
00:06:55,780 --> 00:06:59,460
after the next one, because there's
something else I want to show you -

80
00:06:59,460 --> 00:07:05,300
constants in Python. We'll look at those
in the next video.

