1
00:00:05,040 --> 00:00:09,690
Alright, so let's look now at finishing the
code for our simple jukebox. We've got

2
00:00:09,690 --> 00:00:13,590
our program printing the list of albums,
and getting a choice from the user. The

3
00:00:13,590 --> 00:00:17,220
next bit of code will be very similar.
It's going to display the songs for the

4
00:00:17,220 --> 00:00:22,380
selected album and let the user choose
one. So looking at our albums data,

5
00:00:22,380 --> 00:00:27,880
in nested_data.py, the title of each
song appears at index position 1.

6
00:00:27,880 --> 00:00:32,000
The track number, as you can
see, is at position 0 and the title

7
00:00:32,000 --> 00:00:36,300
at position 1. Rather than having to
remember that, when writing the code,

8
00:00:36,300 --> 00:00:41,260
let's create another constant in jukebox_menu.py.

9
00:00:41,260 --> 00:00:48,860
We'll add it below the existing one - we'll
call this one SONG_TITLE_INDEX

10
00:00:48,860 --> 00:00:51,580
and we're setting it to the value of one.

11
00:00:51,590 --> 00:00:55,760
We don't need line 17 onwards. We just
added those lines to see how we were

12
00:00:55,760 --> 00:01:00,340
indexing into the albums, to get the
song lists. So I'm going to delete those

13
00:01:00,340 --> 00:01:04,540
and then replace that code, and the code
we're going to add is going to display

14
00:01:04,549 --> 00:01:09,560
the list of songs. So I'm going to start
with a print: print parentheses double

15
00:01:09,560 --> 00:01:16,759
quotes Please choose your song and a
colon, closing double quote and right

16
00:01:16,759 --> 00:01:20,930
parenthesis, and notice that error there. I
was a bit careless and wasn't paying

17
00:01:20,930 --> 00:01:27,260
attention. I need to tab this to the
right indentation level, for this to work.

18
00:01:27,260 --> 00:01:31,220
But it's a good idea, just to make sure
that you are indenting to the level

19
00:01:31,220 --> 00:01:37,720
appropriate to the code you're adding. Alright,
also continuing on, for index comma then

20
00:01:37,720 --> 00:01:41,500
in parentheses - hopefully, by now, you
know why we using parentheses -

21
00:01:41,500 --> 00:01:50,700
track_number comma song in
enumerate parentheses songs_list

22
00:01:50,700 --> 00:01:55,240
right parenthesis and a colon. Then
we'll get to the actual printing - print

23
00:01:55,240 --> 00:01:59,740
on the next line, parentheses double
quotes and two replacement fields here,

24
00:01:59,740 --> 00:02:05,120
so left and right curly braces, colon space,
another set of left and right curly braces

25
00:02:05,120 --> 00:02:08,320
double quote, and because it'll all fit on the
same line, I won't go to the nextline

26
00:02:08,320 --> 00:02:14,360
with the dot format, parentheses index + 1 comma song

27
00:02:14,360 --> 00:02:18,940
and the two closing right
parentheses. Alright, so having done that,

28
00:02:18,940 --> 00:02:23,690
and again, we're putting parentheses
around track number and song, because

29
00:02:23,690 --> 00:02:27,290
these are the values that are being
unpacked from the tuple, as we saw in an

30
00:02:27,290 --> 00:02:32,100
earlier video. Alright, next we need to get
the user's song choice and print the title.

31
00:02:32,100 --> 00:02:36,500
So make a bit of space up here, and
at the same indentation level as the

32
00:02:36,500 --> 00:02:41,620
for loop, I'm going to type song_choice is equal to int

33
00:02:41,620 --> 00:02:45,290
parenthesis input, left and right
parentheses and then a closing right

34
00:02:45,290 --> 00:02:55,480
parenthesis. Then we'll test this, so if
1 is less than equal to song_choice

35
00:02:55,480 --> 00:03:02,520
less than equal to len parentheses songs_list

36
00:03:02,520 --> 00:03:09,140
right parentheses : Then we'll type title, 
on the next line, is equal to songs_list

37
00:03:09,140 --> 00:03:12,340
left and right square brackets, and within 
the first set of square brackets,

38
00:03:12,340 --> 00:03:19,140
song_choice - 1, then a second set of square brackets.

39
00:03:19,140 --> 00:03:22,300
And we need to choose the song index, so
that's using our constant,

40
00:03:22,300 --> 00:03:26,880
song_title_index, and we'll put an

41
00:03:26,880 --> 00:03:35,520
else in under the if : and break. Alright, so
once again, we've got two levels of

42
00:03:35,520 --> 00:03:40,709
indexing on line 23. We get the song
tuple from songs list, using the user's

43
00:03:40,709 --> 00:03:45,480
choice as the index. We have to subtract
1, because indexes are zero-based,

44
00:03:45,480 --> 00:03:50,300
and the user's choice starts at 1. To get the
second item from the tuple, we use our

45
00:03:50,300 --> 00:03:55,380
SONG_TITLE_INDEX constant. The name of the song is the second item,

46
00:03:55,380 --> 00:04:02,920
which is at position 1, and if you recall, 
we set our constant value on line 4 to 1.

47
00:04:02,920 --> 00:04:04,500
So before we run the program, we really

48
00:04:04,500 --> 00:04:08,880
should print out the chosen song, to make
sure we got the right one, so let's do that.

49
00:04:08,880 --> 00:04:12,560
So indenting's at the same level
as the if and the else. I'm going to type

50
00:04:12,560 --> 00:04:18,760
print parentheses, Playing, left and right 
curly braces for our replacement field,

51
00:04:18,760 --> 00:04:23,600
double quotes dot format
parentheses title. On the next line,

52
00:04:23,600 --> 00:04:29,080
print parentheses double quotes equal
sign, then outside of the double quote,

53
00:04:29,080 --> 00:04:34,320
x 40 and a right parenthesis. So in
addition to the title that we're

54
00:04:34,320 --> 00:04:38,440
printing on line 27,
I'm also printing a row of equal symbols,

55
00:04:38,440 --> 00:04:45,440
just to make the output a bit clearer. Time 
now to test the code. So I'm gonna run the program.

56
00:04:45,440 --> 00:04:48,940
Alright, clicking into the run window, 
I'm going to select option 3 -

57
00:04:48,940 --> 00:04:53,560
Nightflight.  That's printed the songs from the
Nightflight album, which is correct.

58
00:04:53,560 --> 00:04:59,540
I'm going to choose option 4, here, to print
the song title, She used me up, and you

59
00:04:59,540 --> 00:05:03,400
can see that the program reports that it's
playing She used me up, then the loop goes

60
00:05:03,400 --> 00:05:08,060
around again, ready for the next choice.
So test the program thoroughly, to make

61
00:05:08,060 --> 00:05:13,080
sure valid options display the correct
list of songs, and the correct song.

62
00:05:13,080 --> 00:05:17,120
At the moment, though, we don't have a lot of
data. Because of that, I'd check every

63
00:05:17,130 --> 00:05:20,669
single option to make sure the results
are correct. Also, make sure that an

64
00:05:20,669 --> 00:05:25,050
invalid option correctly exits the
program, and that should happen when

65
00:05:25,050 --> 00:05:29,460
you're displaying the list of albums and
also when a song list is being displayed.

66
00:05:29,460 --> 00:05:32,940
So moving on, the next video will be a
challenge, and I'll go over my solution

67
00:05:32,940 --> 00:05:38,479
to that challenge, in the video after
that one. See you in the next video.

