Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Using a debugger
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So you saw over here that we hard coded the count of the M&Ms so then I could play around with going through it knowing what was happening.
0:09
But we can actually do better than that. Sometimes it's impossible to hard code stuff because you don't control it.
0:15
Maybe you need it from somewhere else or something like that. So what we can do in PyCharm and other editors like Visual Studio Code
0:22
is we can use something called a debugger and debuggers are super, super cool. They're incredibly valuable for helping you understand
0:30
what's happening as your Python code or source code runs. So what I can do is I can scroll over here in between the number and between these texts here
0:39
I can click and it puts a red break point its what it's called and a whole like's kind of followed here.
0:44
So if I just run out like this nothing happened it just run like it did before and so on. But there's another button that kind of looks like a bug
0:55
for debugging this let's go back up here. If I run this it can have a little bit different look down here and it's going to stop right on this line
1:02
right with that break point is you can see it's stuck down here on you know guessing game you see it stop down here on line 10
1:11
just like it says right there. So if I step over if I go one line forward using this see this step over this means just go down this file one at a time
1:22
as opposed to go try to go inside this library and see what it's doing which would be this one and click this
1:28
and now, see that little gray piece up here M&M count is 26 and then down here M&M count is 26.
1:35
I can even go over here and change the value to something other than 26 if I wanted but let's go along a little further.
1:42
Oops I must've hit the wrong button. So now the count is 79 so when we go down here and notice we've gone our, while attempts are like we say
1:51
I'm going to step notice attempt limit is 5 current attempt is this we step one bit and then we going to go over here
1:57
and notice the console is asking for input we actually know the answer is 79 but I'm going to just say it's 70
2:05
and notice here's the guest text coming in and then we're going to go and convert that to a guest number now 70 without the quote
2:14
and I'm going to increment the attempt notice it goes orange appear that means it's value is recently changed so it's orange
2:21
because we change it right there. Now we can compare M&M Ncount 79 against guest 70 and let's see what happens says no that's not true
2:30
so to the next test sorry the guest is too low that will come out here. Now we're going to do another test one is still less than the limit
2:38
so let's keep going we go back to the debugger and see the values. When it's asking for the user input it's like hides all its values so let's put 80
2:48
and now it comes back and will show its value again we can see out attempt is 2 our guest is 80 and let's just keep going that's too high.
2:56
Let this time get it right and over here. 79. Alright, 79 and let's convert that to 79 right there. One more attempt, it's going to go from
3:08
notice up here from 2 and it's going to shown orange it go to three now it's going to be right so perfect. We're going to say you got this free lunch
3:15
and then break should take us down the line 26 empty search prior go to 27. Boom, there it is. We were done in three attempts.
3:24
Stop, that's the end of the program. That is super, super viable if you're having problems with your code working right you're doing a test
3:33
and it's not quite doing what you're expecting or even you're getting the error. So this debugger is extremely easy.
3:39
Click here, click there and you're going in PyCharm and yet it's really, really powerful to help you understand how you're code is executing
3:48
and what the values are at any moment.