1
00:00:04,620 --> 00:00:09,960
In this video we'll see how the debugger,
built it into IntelliJ and PyCharm,

2
00:00:09,960 --> 00:00:14,190
can help us to understand what our code's
doing. We've seen how to run the code by

3
00:00:14,190 --> 00:00:19,770
right-clicking in the editor pane, like
so, and selecting run, but just below that

4
00:00:19,770 --> 00:00:24,480
there's this debug option. Now just
before I click that, I want to talk about

5
00:00:24,480 --> 00:00:29,420
an option that might come up on the
screen like you'll see here for Windows users.

6
00:00:29,420 --> 00:00:31,940
So if you see this dialog and you're on windows,

7
00:00:31,940 --> 00:00:35,800
either now or at some other
point when using your IDE, then it's very

8
00:00:35,800 --> 00:00:39,760
important that you tick the box to allow
access on private networks, as you can

9
00:00:39,760 --> 00:00:42,760
see checked on the screen.

10
00:00:43,400 --> 00:00:47,160
Make sure that's checked and then click on
allow access. Failing to do that, well,

11
00:00:47,170 --> 00:00:51,340
you'll find that, basically, IntelliJ or
your IDE whatever you're using won't be

12
00:00:51,340 --> 00:00:54,390
able to access the Internet.

13
00:00:54,450 --> 00:00:57,780
Now on a Mac, although I haven't got a
screenshot for this, you may notice

14
00:00:57,780 --> 00:01:01,120
there's a little message that can appear
down the bottom right-hand corner,

15
00:01:01,120 --> 00:01:05,239
suggesting you install a Cython
extension. Now if you're using a

16
00:01:05,250 --> 00:01:09,030
different implementation of Python, other
than what we've suggested you install in

17
00:01:09,030 --> 00:01:13,500
this course, you won't see that, but with
the CPython implementation on a Mac,

18
00:01:13,500 --> 00:01:17,820
you should see a pop up at some point.
But I suggest you ignore it and if you

19
00:01:17,820 --> 00:01:21,210
do that the dialog will disappear after
a short while. Now in this particular

20
00:01:21,210 --> 00:01:24,869
extension on a Mac, it can be really
useful if you've got a large code base but

21
00:01:24,869 --> 00:01:28,649
at the moment, the code in this course
isn't complex enough to benefit from

22
00:01:28,649 --> 00:01:32,190
installing another extension. It's really
just introducing another potential point

23
00:01:32,190 --> 00:01:37,080
of failure, so for that reason I suggest
you ignore it. Alright, so back to the

24
00:01:37,080 --> 00:01:43,180
screen now. I'm going to click on Debug
instead of Run. Now you can see what's

25
00:01:43,180 --> 00:01:48,820
happened there, we've got some text
showing pydev debugger: process 4672  is connecting.

26
00:01:48,820 --> 00:01:51,440
Maybe we'll say
connected instead of connecting.

27
00:01:51,440 --> 00:01:54,479
Now ignore the process ID. That number will
probably be different for you, and in

28
00:01:54,479 --> 00:01:58,229
fact, will be different every time you're
running it. Now notice that we've got a

29
00:01:58,229 --> 00:02:02,549
debug pane at the bottom of the screen,
rather than a run pane, and that will

30
00:02:02,549 --> 00:02:06,600
come up when we're actually using this
debugger. Now the debugger itself has got

31
00:02:06,600 --> 00:02:10,289
two panes. We've got the debugger and the
console, and you can see at the moment,

32
00:02:10,289 --> 00:02:14,220
the console pain is checked and that
just shows the output from your program,

33
00:02:14,220 --> 00:02:17,700
just like it did when we were running
programs previously.

34
00:02:17,700 --> 00:02:22,460
It's basically the same output if you're running it using
the run option, and we'll see the

35
00:02:22,460 --> 00:02:26,540
debugger tab in a moment. But what I'm going
to do now is just enter my details.

36
00:02:26,540 --> 00:02:31,440
Down here I'm going to type in my name and an
age, and you can see the program runs,

37
00:02:31,440 --> 00:02:35,130
just as it did before in the previous
video. But where the debugger gets

38
00:02:35,130 --> 00:02:39,920
interesting, is when we set breakpoints
in our code. A breakpoint tells the

39
00:02:39,920 --> 00:02:44,260
debugger to pause at that point. This
allows us to see what the code's doing

40
00:02:44,260 --> 00:02:48,700
and step through, line by line. The good
news is that setting a breakpoint's easy.

41
00:02:48,700 --> 00:02:51,930
All you need to do is just click in
the left margin on the line you want to

42
00:02:51,930 --> 00:02:56,010
set the breakpoint at. So I'm going to
set one over here on line 11, clicking in

43
00:02:56,010 --> 00:02:59,500
the margin there, and you can see when I
did that, the line's highlighted and we've

44
00:02:59,500 --> 00:03:05,160
got now a red dot in the left margin. To
clear a breakpoint, click it again,

45
00:03:05,160 --> 00:03:07,780
and you can toggler breakpoints on and off
throughout your code.

46
00:03:07,780 --> 00:03:16,060
I'm gonna set that one back on line 11. Now that I've done that, I want to use the debug option again.

47
00:03:16,060 --> 00:03:20,140
The code will stop when it
reaches that breakpoint. Now to get there,

48
00:03:20,140 --> 00:03:23,040
because we were executing the code at
the moment above that,

49
00:03:23,040 --> 00:03:26,660
we're executing the code at the moment on line 1, so
we need to enter some information until

50
00:03:26,660 --> 00:03:33,320
the code gets to that point. So I'm going to type in Tim and 30.

51
00:03:33,320 --> 00:03:37,620
You can see what happened when I pressed 30 there. The program's now got up to line 11,

52
00:03:37,620 --> 00:03:41,100
and it's waiting there for us to do
something. Now in the debugger tab of the

53
00:03:41,100 --> 00:03:46,140
debug pane, you can see the value of our
two variables there, on the right hand side.

54
00:03:46,140 --> 00:03:49,410
Now the debugger tab normally opens
automatically, as you saw that it did

55
00:03:49,410 --> 00:03:52,590
here in my case, but you can switch
between the debugger and the console

56
00:03:52,590 --> 00:03:57,560
tabs by clicking them. So I'll come over here and click on console, and there's the output.

57
00:03:57,560 --> 00:04:02,440
I'll go back to debugger for now. So
looking at the left hand pane in the

58
00:04:02,459 --> 00:04:07,110
debugger tab, it can look a tad confusing.
The good news is we can ignore all of

59
00:04:07,110 --> 00:04:10,470
that at the moment. Just make sure that
you've got the top option blocks

60
00:04:10,470 --> 00:04:13,900
selected, and you can see at the top
that it is selected for me.

61
00:04:13,900 --> 00:04:19,420
Now to the right of the console tab, note that we've
got a mini toolbar with a few buttons.

62
00:04:19,420 --> 00:04:23,700
The first one, the three horizontal bars
that represent lines of code, shows the

63
00:04:23,700 --> 00:04:27,960
current line that will be executed. Now
where that is useful is in larger programs,

64
00:04:27,960 --> 00:04:31,950
or programs using several files. We've
only got a few lines of code but we can

65
00:04:31,950 --> 00:04:35,720
demonstrate what it does. If I can do
that by going back up to line 1,

66
00:04:35,720 --> 00:04:40,919
say to there. If our current execution line
was off the screen, say, this button is a

67
00:04:40,920 --> 00:04:45,060
quick way to get back to it, so I wanna
come down here and click on that button.

68
00:04:45,060 --> 00:04:48,980
You can see the cursor  went down to
line 11, not particularly useful as a

69
00:04:48,990 --> 00:04:52,620
small file, but very much useful if
you've got larger files or multiple

70
00:04:52,620 --> 00:04:56,250
files that you're working on. Now we're
going to talk about the last button next,

71
00:04:56,250 --> 00:05:00,030
this one over here. It's the last one
that's available anyway. You can see that

72
00:05:00,030 --> 00:05:04,240
it looks a little bit like a calculator,
and it lets you evaluate expressions.

73
00:05:04,240 --> 00:05:09,300
Now our code is about to execute line 11 and
hasn't executed it yet. I'm just going to

74
00:05:09,300 --> 00:05:12,460
move that code down so you can see a bit
more of it, and then I'm going to click on

75
00:05:12,460 --> 00:05:15,840
that little button,
Evaluate expressions. I'm going to move

76
00:05:15,840 --> 00:05:19,620
this over to the right so we can still see the code and see this at the same time.

77
00:05:19,620 --> 00:05:21,620
And under expression I'm going to
type

78
00:05:21,630 --> 00:05:28,200
age less than 18. I come down here, click
on evaluate, and when I do that note that

79
00:05:28,200 --> 00:05:32,160
we've got result equals, any left and
right clear braces, bool being the type,

80
00:05:32,160 --> 00:05:37,620
and then the value, in this case, false. So
looking at our code, we can see now that,

81
00:05:37,620 --> 00:05:42,580
given that age less than 18 is false,
that line 12 won't be executed.

82
00:05:42,580 --> 00:05:47,900
That's because the expression on line 11 is going to
evaluate to false. I suggest you do this,

83
00:05:47,900 --> 00:05:51,600
when using the debugger, as a
learning tool. Don't just click the next

84
00:05:51,600 --> 00:05:55,290
button and watch the code executing. Take
the time to read the code and work out

85
00:05:55,290 --> 00:05:58,920
what it's going to do. When you think
you've worked it out, then you can step

86
00:05:58,920 --> 00:06:02,940
through the code to see if you're right.
A great way to learning, really solidify

87
00:06:02,940 --> 00:06:06,210
the information you're learning in this
course. Now if you just mindlessly click

88
00:06:06,210 --> 00:06:08,880
the button to run the next line, which
I'll talk about shortly,

89
00:06:08,880 --> 00:06:12,160
you won't really learn very much. You'll
learn how to mindlessly click a button,

90
00:06:12,160 --> 00:06:15,580
but we're learning to become programmers
here, not mindless button clickers.

91
00:06:15,580 --> 00:06:21,400
Alright, so moving forward, we know that line 12
won't be executed. Next comes line 13,

92
00:06:21,400 --> 00:06:26,060
though that's going to test the
condition; age is equal to the value of 900.

93
00:06:26,060 --> 00:06:29,400
Now we really don't need the
expression evaluated to work that one out,

94
00:06:29,400 --> 00:06:33,780
because we can clearly see age in
the debugger variables pane there,

95
00:06:33,780 --> 00:06:37,800
at the bottom of the screen, showing the
value of 30. But we're seeing how to use it

96
00:06:37,800 --> 00:06:43,660
so I'm going to enter the expression
anyway; age equals equals 900.

97
00:06:43,660 --> 00:06:48,060
Note also, if I type a variable now that doesn't
exist, we'll actually get an error.

98
00:06:48,060 --> 00:06:51,060
So it's a good way to confirm that what
your typing is valid, so age is equal to

99
00:06:51,060 --> 00:06:57,240
equal, is equal to 900. I was typing 1800
instead of  900. Same do there, I can

100
00:06:57,240 --> 00:07:02,740
click on evaluate, and you can see the result
down the bottom, again, is showing false.

101
00:07:02,740 --> 00:07:07,640
Just to confirm that, I could change this,
result is equal to 30, evaluate.

102
00:07:07,640 --> 00:07:11,880
You can see the value there is true. So I can see
how this is a very useful tool. Well I'll

103
00:07:11,880 --> 00:07:16,350
set that back to 900. Click on evaluate,
we get false again. Basically, that's

104
00:07:16,350 --> 00:07:21,440
telling us also that the code on line 14
won't be executed either.

105
00:07:21,440 --> 00:07:25,220
Alright, so there's no other conditions in our short
little program to check, and none of the

106
00:07:25,220 --> 00:07:28,940
ones we've checked were true. So the only
thing left now is the else block,

107
00:07:28,940 --> 00:07:33,240
so that's what's ultimately going to be
executed on line 16. So let's see

108
00:07:33,240 --> 00:07:37,960
if we're right. I'm gonna, firstly,  close
the Evaluate pop up.

109
00:07:37,960 --> 00:07:40,680
Let's have a look at some of
these other buttons here, to the right of

110
00:07:40,680 --> 00:07:45,260
this little button here that we've already talked about, the Show Execution Point.

111
00:07:45,260 --> 00:07:47,840
The two buttons we're going to
look at at the moment, are this one here,

112
00:07:47,849 --> 00:07:51,180
which is a step over and step into. We
not really going to look at this other

113
00:07:51,180 --> 00:07:55,289
one, step into my code, at this point.
Specifically, it's these first two which

114
00:07:55,289 --> 00:07:59,520
appear to do the same thing, and we'll
actually look at step into when

115
00:07:59,520 --> 00:08:02,220
we actually start writing our own functions.
For now though, I'm going to come over here

116
00:08:02,220 --> 00:08:06,660
and click on step over, noting that we've
got a shortcut key we could use instead

117
00:08:06,660 --> 00:08:11,240
of clicking, if we prefer. So I'm going to
click that once. Notice when I do that,

118
00:08:11,240 --> 00:08:16,820
the code on screen has jumped to line 13.
So line 12 didn't get executed, as we expected,

119
00:08:16,820 --> 00:08:21,540
and we're at the elif statement on line 13. I'm going to click the button again

120
00:08:21,540 --> 00:08:24,920
and see what happens with this
expression, and you can see also we were

121
00:08:24,930 --> 00:08:28,289
right again. In this case, line 14
wasn't executed. Instead we've skipped

122
00:08:28,289 --> 00:08:33,929
straight down to the else code on line
16. Basically, we knew that both of those

123
00:08:33,929 --> 00:08:38,429
expressions evaluated to false, and
we've got confirmation of that by

124
00:08:38,429 --> 00:08:43,289
clicking on the step over button. Now we
can also confirm that nothing's been

125
00:08:43,289 --> 00:08:47,260
output, by clicking over here to the
console tab so I'll do that now.

126
00:08:47,260 --> 00:08:50,940
And notice we've got the two bits of
information that I typed in but we've

127
00:08:50,940 --> 00:08:56,720
got no other output at this time. But if
I click on step over one more time now,

128
00:08:56,720 --> 00:08:59,640
you can see that caused line 16 to
execute. We've got the message 'You're old

129
00:08:59,640 --> 00:09:02,880
enough to vote' printed it in the
console. Clicking it one more time

130
00:09:02,880 --> 00:09:07,860
will execute the last line and then end the
program. You can see that's what's happened.

131
00:09:07,860 --> 00:09:11,480
We've got the message 'put an X
in the box' and the program terminates.

132
00:09:11,480 --> 00:09:15,080
Alright, so that's the debugger. Now we're
going to be using the debugger a lot in

133
00:09:15,089 --> 00:09:19,050
the early sections. I don't recommend
using one for real debugging unless

134
00:09:19,050 --> 00:09:23,010
you're totally stuck, but they're a great
way to watch your code executing while

135
00:09:23,010 --> 00:09:26,490
you're learning about it. Experiment with
setting breakpoints and stepping through

136
00:09:26,490 --> 00:09:30,750
your code to get used to using the debugger. Now before I finish, there's another

137
00:09:30,750 --> 00:09:34,350
button I want to show you. It's very
useful if you do get stuck, if you step

138
00:09:34,350 --> 00:09:38,360
too far, for example, or end up
debugging the Python libraries.

139
00:09:38,360 --> 00:09:43,860
So I'm going to run the program in the debugger one more time.

140
00:09:43,860 --> 00:09:47,880
So notice the Red Square over to the left hand side of the screen, down the bottom.

141
00:09:47,880 --> 00:09:51,100
That's used to stop execution,
allowing you to start again if you

142
00:09:51,100 --> 00:09:54,880
get caught or get into a mess. You might
get some errors when you stop the program,

143
00:09:54,880 --> 00:09:58,080
but I'm going to click on that
now. Now I've only got a note there

144
00:09:58,089 --> 00:10:01,629
saying the process finished with exit
code minus one, but if you do get some

145
00:10:01,629 --> 00:10:05,740
errors when you stop the program, that's
fine. Ignore any errors in that scenario.

146
00:10:05,740 --> 00:10:08,460
That's completely okay when you're
terminating the program, as we've done there,

147
00:10:08,460 --> 00:10:12,620
while stopping it. So with that said, I'm going to finish now, and in the next video

148
00:10:12,620 --> 00:10:18,380
what we're going to do is start taking a look at more on if, elif and else.

149
00:10:18,380 --> 00:10:21,420
See you in the next video.

