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