Using the PyCharm Debugger Transcript
[00:00:00]
Hey there! I’m going to show you how to use the debugger that’s built into
PyCharm so that you can step through your code and understand what is
happening at any point in time in your program.
Start visual description. Code on the screen reads:
def get_age():
age = input(“And what is your age? ’) ? return age
def print_message(name, age):
print (f’{age} years ago, {name} was born!‘)
def main():
name = get_name()
age = get_age()
print_message(name, age)
End visual description.
[00:00:12]
This is a skill that you will use throughout your career as a programmer as a way
of finding problems in your code or things that you wrote aren’t doing quite what
you wanted them to do and this is a tool that you used to identify those
problems, that helps you then solve and fix those problems.
[00:00:32]
So I’ve got this little script up here that has a number different functions. A
simple, you know, get some information and print. And we can inspect and step
through this code this way. So next on the left side of your editor this is what we
call the gutter.
[00:00:56]
There’s the play button is in the gutter if I click in an empty spot in the gutter
these red dots appear. That’s what we call a break point. And it’s very similar to a
snapshot in bit. When I put a break point in, when I run my code with the
debugger as opposed to the normal run method then the program will actually
stop at that breakpoint and allow me to look at it and see what’s going on.
Start visual description. Instructor clicks on the space near the line number on the code to create a breakpoint. End visual description.
[00:01:26]
So in the past, we’ve used the run button but now we can use the debug button
instead. You can also get to that just clicking on the run, the play button, and
selecting debug here. You can right click and say debug. All the same thing. And
now this debug window opens up.
[00:01:47]
So it’s a little bit different than the Run window. It has more things in it. When
you very first open the debugger it looks something like this, where you have the
debugger here and then you have the console here and this is where it will print
output and expect input to the program.
[00:02:07]
The debugger shows you where in the code you are and what the variables are. I
found that this doesn’t work very well for me to have these two separate tabs
and so I recommend you take the console tab, click it, and then just start
dragging it over to here.
[00:02:24]
And that will allow you to resize this little bit. See the console on one side and
the debug information on the other side. Alright. So with that in place, moving
the console over to the side, I can see both the console input and output and I
can see what the debugger wants to show me.
[00:02:52]
So what does this do? Once I hit this breakpoint… so right now it says I’m paused
at line 22 right here where this breakpoint is. And I can use these buttons to step
around in my code. First we want to look at, is the step into my code button.
[00:03:13]
You’ll notice it looks very similar to the step into button that little lines next to it.
You always want to use the step into my code, that’s the relevant one here. So if I
step into my code, it’s going to step into the main program, the main function,
right here and stop at line 16.
[00:03:31]
If right here I choose to use the step over, then it will run the get name function
which calls input and now it prints out “Hello, what is your name?” and here it’s
telling me it’s waiting for me to say something, right. So I’ll say Dr. Bean. Now
that I provide a name, Dr. Bean, get name receives it, returns it, which returns
here and now it’s telling me that name, the value of name, right now is Dr. Bean.
I see that also down here. Name equals Dr. Bean.
Start visual description. The console text reads: Hello, what is your name? Instructor types in Dr. Bean. End visual description.
[00:04:08]
So as you’re stepping through the debugger, all of your variable values will listed
here in this middle panel. Alright, so if I step over again, it will run the get age
function, which pauses while it waits for me to provide some input. What is your
age? Old.
Start visual description. The console text updates to: What is your age? Instructor types in old. End visual description.
[00:04:30]
And so now it will return that to age. Right now I can see age is old. It also puts it
up here on the screen so it’s easy to see. And then if I want, I can step over print
message or I could step into print message instead of stepping over it. And now I
can see, that you know, it’s paused right here before this print function. It tells
me that in this function, name is Dr. Bean and age is old.
Start visual description. The console text reads: old years ago, Dr. Bean was born! End visual description.
[00:04:51]
Step. It prints it out. Finishes print message. Finishes main function. Finishes the
program and it stops. So let’s run that again just to see this process in action.
Alright, so I debug, it will spin up the program. Starting from a debugger usually
takes just a moment longer than running the program.
[00:05:15]
So now we’re paused here. Let’s step into main. Let’s step into get name. Step
into. So this is the line that’s about to run. It hasn’t run yet, but as soon as I step
it’s running the input function now, which prints this out and is now waiting for
something. Right, orange.
[00:05:33]
Great, so now it wants to return, right. So I can step and it comes back out of this
function. Here return values is enabled. To see this, you click on the little gear
settings icon and say show return values.
Start visual description. The console text reads: Hello, what is your name? Instructor types in Orange. End visual description.
[00:05:51]
And what that shows me here is get name is returning orange. So now as I take
the next step, name now got orange that get name returns to it. Right? I can step
into get age. And step. What is your age? Seven. Return the age. So I can see
under return values, get age is returning a seven.
[00:06:15]
And now we’re going to store that in the age value. Right, now we can print that
out. Let me see, seven years ago, Orange was born. Great. And finished program.
You can put break points in other places as well. So for example, instead of main,
we could say, let’s put a break point right here before print message.
Start visual description. The console text reads: What is your age? Instructor types in 7. The console then reads 7 years ago, Orange was born! End visual description.
[00:06:39]
Now if I run this, then the program will start right away. And you can see here it’s
printed out “Hello, what is your name?” Yes. What is your age? Yes. Right. Name
is yes, age is yes. Now it’s going to print message. And it’s paused at this break
point. So you can put break points wherever you want.
[00:07:00]
I can say stop and just stop the program. I could put a break point right here at
the beginning of the function and one here. You can have more than one. So now
we’ve paused right before get name happens. Now I can hit the resume program
button and it’ll start running again. Hello, what is your name? Bean. What is your
age?
Start visual description. The console reads What is your age? Instructor types in yes, and then an error message is shown. End visual description.
[00:07:27]
Yes. And now it’ll pause again because it hit the second break point. So just like
bit snapshots where you can have more than one and jump between them, with
a debugger you can set multiple break points for where you want to stop and
look at what’s going on in your program.
[00:07:45]
Unlike bit, you can’t step backwards. You can only step forwards. But you can
have as many breakpoints as you need in your program. Using this middle panel
helps you see what’s going on.
[00:08:01]
What’s the state of your program? What data do you have? What variables are
active and what values do they have? And the console let’s you see what’s
getting printed and expected. And so using that, you can figure out if there’s an
error. You can often identify where that error is. It’s a powerful tool.
[00:08:21]
I encourage you to get used to this process of putting in break points and
stepping around. As the semester goes, you’ll use that in order to identify
problems in your code. And with that, thanks for watching. Enjoy this lab!