1
00:00:04,660 --> 00:00:09,400
In this video, we're going to add a bit
more code to our simple jukebox program.

2
00:00:09,400 --> 00:00:13,070
We've got it printing the menu of albums,
and now we need to add the menu of songs

3
00:00:13,070 --> 00:00:17,360
for a chosen album. We won't finish the
program in this video, though.

4
00:00:17,360 --> 00:00:21,919
That's because I want to use this code to talk
about constants in Python. I'll talk

5
00:00:21,920 --> 00:00:25,500
about them in a moment. First though, let's tidy
up our code and add a bit more.

6
00:00:25,500 --> 00:00:31,080
We don't need that second for loop, so I'm going
to remove that. So our menu should only

7
00:00:31,080 --> 00:00:34,420
show the album name. At the moment,
we're printing all the details for an album,

8
00:00:34,420 --> 00:00:37,820
which makes the menu a bit
cluttered. So let's remove the extra

9
00:00:37,820 --> 00:00:45,280
placeholders on line 6, so that we've
only got a total of 2 there.

10
00:00:45,280 --> 00:00:50,920
We're only going to output index + 1 plus title, so we're going to remove artist, year and songs

11
00:00:50,920 --> 00:00:55,900
from line 7, like so.
Now that's shortened the line quite

12
00:00:55,900 --> 00:00:59,080
considerably, and in fact, that'll fit
on a single line now, so let's go back

13
00:00:59,080 --> 00:01:03,460
and do that, like so.
Alright, so let's run that just to make

14
00:01:03,469 --> 00:01:07,490
sure that I haven't broken anything. It's
a good idea, actually, to run your program

15
00:01:07,490 --> 00:01:11,320
often, when developing it. That way, if you
get an error, you only have to look at

16
00:01:11,320 --> 00:01:14,869
the small amount of code that you've
just changed. If you type in thousands of

17
00:01:14,869 --> 00:01:18,140
lines of code before running it, you could
have all sorts of bugs and it'll be very hard

18
00:01:18,140 --> 00:01:20,460
to try and figure out where the problem is.

19
00:01:20,460 --> 00:01:23,360
Alright, so you can see there when I ran
it, we get our albums menu printed out,

20
00:01:23,360 --> 00:01:26,899
which is just what we wanted. Next what
we need to do, is get some input from the

21
00:01:26,899 --> 00:01:32,149
user, then check that it's a valid choice.
So let's add some code for that.

22
00:01:32,149 --> 00:01:36,890
So I'm gonna start down here on line 8. I'm
gonna type choice - and I'm indented to

23
00:01:36,890 --> 00:01:43,790
the same level as the for loop -
choice equals int parentheses, and within

24
00:01:43,790 --> 00:01:47,509
the parentheses, input left and right
parentheses. So obviously,

25
00:01:47,509 --> 00:01:51,020
as always we've got our closing right
parenthesis. Then on line 9, I'm going to

26
00:01:51,020 --> 00:02:00,440
type if 1 is less than equal to choice,
less then equal to len albums,

27
00:02:00,440 --> 00:02:08,259
right parenthesis : On the next line, songs_ list
 is equal to albums

28
00:02:08,259 --> 00:02:11,980
left square brackets, and within the square
brackets, choice

29
00:02:11,990 --> 00:02:15,830
- 1, then we've got our right
square bracket then the three in square

30
00:02:15,830 --> 00:02:20,900
brackets, after that. Then on the next
line - line 11, indented to the same

31
00:02:20,900 --> 00:02:26,940
level as the if, I'm going to type else :
then go down to line 12 and indent break

32
00:02:26,940 --> 00:02:31,400
to the next level.
Alright, so on line 8, we get input

33
00:02:31,400 --> 00:02:35,510
from the user and convert it into an
integer. Our program will crash if the

34
00:02:35,510 --> 00:02:39,050
user types anything other than digits. As
I said, we'll be looking at how to deal with

35
00:02:39,050 --> 00:02:42,770
that, later in the course. Line 9's
checking that the value they entered is

36
00:02:42,770 --> 00:02:48,860
within the required range. Our valid choice
is from 1 to the length of the albums list.

37
00:02:48,860 --> 00:02:52,500
We've got four albums, so any value
greater than four is invalid.

38
00:02:52,500 --> 00:02:57,520
Zero is also invalid. If they enter an
invalid choice, we break out of the loop

39
00:02:57,520 --> 00:03:03,420
via the else statement. Line 10 is
binding a variable called songs_list

40
00:03:03,420 --> 00:03:06,740
to the list of songs for the
selected album. If you find yourself

41
00:03:06,740 --> 00:03:11,500
struggling with nested indexing like this,
a good technique is to print the values out.

42
00:03:11,500 --> 00:03:16,700
Let's do that. We're going to indent the code, starting on line 14, to the same level as the if.

43
00:03:16,700 --> 00:03:21,980
So print parentheses albums,
then left and right square brackets and

44
00:03:21,980 --> 00:03:27,000
within the square brackets, we're going
to type choice - 1, then we've got our

45
00:03:27,000 --> 00:03:31,160
closing right square bracket and
right parenthesis. On the next line,

46
00:03:31,160 --> 00:03:36,020
also indented at the same level, print
parentheses songs_list and

47
00:03:36,020 --> 00:03:42,800
finally, line 16, print, and just a set of
parentheses to print an empty line.

48
00:03:42,800 --> 00:03:48,920
Run the program, and what we might do just to see
this a little bit clearer, we're going to

49
00:03:48,920 --> 00:03:53,510
change the View Mode again, as I've done
previously, to Window. So this time we're

50
00:03:53,510 --> 00:03:56,990
going to select option 1,
Welcome to my Nightmare. We can see that

51
00:03:56,990 --> 00:04:04,850
we've got our menu printing out. So
you can see, we've got the tuple for

52
00:04:04,850 --> 00:04:09,620
album zero printed out on screen, there.
Choice is 1, and we're subtracting one

53
00:04:09,620 --> 00:04:15,770
from that on line 14. Now on line
10, we bind songs_list to

54
00:04:15,770 --> 00:04:20,149
the fourth item in the tuple. We're
using an index of 3 which gives us the

55
00:04:20,149 --> 00:04:24,940
fourth item - that's the list of songs - and
that's what line 15 is printing out,

56
00:04:24,940 --> 00:04:28,940
and we can see that showing
in the output pane. So choose some other

57
00:04:28,940 --> 00:04:32,539
options, and make sure that you're happy
with how songs_list gets

58
00:04:32,539 --> 00:04:36,919
the correct list of songs for each album,
before continuing. So pause the video and

59
00:04:36,919 --> 00:04:42,110
come back when you've finished
experimenting. Alright, so before we write

60
00:04:42,110 --> 00:04:46,780
the rest of the code, I want to talk
about constants in Python. Now this is

61
00:04:46,780 --> 00:04:50,720
going to be quite a short program, but
often you'll be working with hundreds, or

62
00:04:50,720 --> 00:04:54,650
even thousands of lines of program code.
With large programs, it can be very

63
00:04:54,650 --> 00:05:00,020
difficult to remember what everything is.
So looking at line 10, for example, we

64
00:05:00,020 --> 00:05:04,700
have to remember that the list of songs
is at index position 3, in albums.

65
00:05:04,700 --> 00:05:09,100
That would be much easier if we gave 3 a
more meaningful name. We could call it

66
00:05:09,100 --> 00:05:15,540
songs list. So let's go ahead and actually
do that, and exit out of our program.

67
00:05:15,540 --> 00:05:20,620
Back up here, where the while loop
currently is, I'm going to type on line 3,

68
00:05:20,620 --> 00:05:24,680
songs_list equals 3 - put an extra

69
00:05:24,680 --> 00:05:29,630
blank line there. Now we can use the
variable, instead of remembering that we

70
00:05:29,630 --> 00:05:34,880
want the value 3. The problem with that
is, the value must always be 3. It's a

71
00:05:34,880 --> 00:05:39,080
constant that refers to the fourth item
in the list. If you go changing the

72
00:05:39,080 --> 00:05:44,120
value that songs list is bound to, our code
won't do what we expect. We want

73
00:05:44,120 --> 00:05:48,140
songs list to represent a constant value.

74
00:06:30,760 --> 00:06:35,420
So looking at our code, we've reassigned songs_list on line 12.

75
00:06:35,420 --> 00:06:39,119
That's not good and it will cause our code to break. So many languages

76
00:06:39,119 --> 00:06:43,740
have some way of marking a value as
constant. The keyword const, c o n s t,

77
00:06:43,740 --> 00:06:47,380
is used in C, for example. In Java, the
keyword is final.

78
00:06:47,380 --> 00:06:50,520
Python, on the other hand, doesn't have
any way of preventing us from changing

79
00:06:50,520 --> 00:06:54,980
the value of a constant. Instead, there's
a convention that constants should have

80
00:06:54,990 --> 00:07:00,270
names that are in all capitals. So let's go
ahead and make a change here, on line 3,

81
00:07:00,270 --> 00:07:04,319
and make it SONGS_LIST in
uppercase, as you can see there, leaving

82
00:07:04,320 --> 00:07:08,660
it still to the value of 3. Whenever you
see a name that's all in capitals, that

83
00:07:08,660 --> 00:07:13,020
means it represents a constant and you
shouldn't change it. Now you'll find that

84
00:07:13,020 --> 00:07:20,440
convention is in the Python Style sheet,
which I'll quickly open up now.

85
00:07:20,440 --> 00:07:25,140
The link, as always, is in the resources section. Note that this is only a convention, though.

86
00:07:25,140 --> 00:07:28,499
The Python compiler won't stop
you reassigning a constant -

87
00:07:28,500 --> 00:07:31,560
it's ultimately your responsibility to make
sure that you don't do that.

88
00:07:31,560 --> 00:07:37,500
Python 3.8 adds a Final annotation, and
a final qualifier to the typing module.

89
00:07:37,500 --> 00:07:41,000
However, the compiler still doesn't
prevent you from changing the value of a

90
00:07:41,009 --> 00:07:44,849
constant, so pay attention to this
convention. Use uppercase for the names

91
00:07:44,849 --> 00:07:50,219
of your constants, and don't change any
variable that's in uppercase. Alright, so

92
00:07:50,220 --> 00:07:56,200
back to the code. We can now use our
constant on line 12, in place of the value 3.

93
00:07:56,200 --> 00:08:01,040
I'm gonna do just that:
SONGS_LIST, as you can see there,

94
00:08:01,040 --> 00:08:04,580
Because variable names are case
sensitive, SONGS_LIST in

95
00:08:04,580 --> 00:08:09,540
square brackets at the end of that line,
represents a different value to songs_list

96
00:08:09,540 --> 00:08:13,100
at the start, which isn't
in uppercase. If you want to be even

97
00:08:13,110 --> 00:08:16,849
clearer, you could come up here and
refactor this. Let's go ahead and do that:

98
00:08:16,849 --> 00:08:25,720
Refactor, Rename, and we'll charge it from
SONGS_LIST to SONGS_LIST_INDEX,

99
00:08:25,720 --> 00:08:27,800
and you can see the change has been reflected

100
00:08:27,809 --> 00:08:31,979
on line 12, as well as line 3. That's a good
idea - now the name more accurately

101
00:08:31,980 --> 00:08:36,360
reflects what the constant's for.
So that's constants in Python.

102
00:08:36,360 --> 00:08:39,779
There's nothing special about them - they're just
variables. The compiler doesn't treat

103
00:08:39,779 --> 00:08:43,440
them any differently, to any other
variable. We write them in capitals to

104
00:08:43,440 --> 00:08:47,220
let other humans know, that they
represent a value that shouldn't be changed.

105
00:08:47,220 --> 00:08:51,560
Alright, so in the next video, we'll 
finish the code for our jukebox menu.

106
00:08:51,560 --> 00:08:54,560
See you in the next video.

