Pycharm Tutorial #2 - Debugging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another pycharm tutorial video in today's video we're gonna be talking all about debugging and the way that PyCharm can help you debug your code and save you a lot of time and a lot of headache while coding so the PyCharm debug tool is extremely useful and extremely powerful when I first found out about it it kind of blew my mind because it was allowing me to do things that I never would have been able to do in idle and save me tons of time so first of all the debug tool is up here in this top right hand corner it's right beside the run it looks like a little bug which makes sense so if you click it it's gonna bring up your basic console and it's gonna run the program just like it would before the only difference is you get this little debugger tab and this is what I'm going to be showing you how to use today there's a lot of cool tools in it you can hover over some of the stuff and kind of play with it for yourself if you don't believe me so I'm just gonna end the program for right now because I want to show you how to add breakpoints first before we can really use this debug tool to its full potential so when you're debugging code a lot of the time you know like we're loop is or like where a statement is that's giving you some issues so if you want to check out that statement you want to look closer into it you might want to add something called a breakpoint and this is how we're gonna be doing things with the debug tool so you can see I have a little bit of code here I have one function two functions and pretty much all this does is it's just gonna get two random numbers and it's gonna ask you to tell what the multiplication of those numbers is if you get a crack tells you correct if you get incorrect it tells you incorrect and so on so it add a breakpoint in pycharm you this thing called the gutter on the left hand side and you can see beside the line number if you click then you can add a little red dot and to remove it you just click on it again so what I'm going to do is I'm just gonna add one at this if statement here and I kinda just have to show you by example how this works so when I start running the function just notice it's gonna run this main function ten times when I run we're gonna go in this while loop it's gonna ask me a question and then it's gonna check if this is a digit so let's just run this in the debug tool and see what happens okay so I'm clicking debug it asked me to type in a number so we're not yet at this line because we're still asking for M so let's type in something let's type 56 and now you can see that my console moves to the side and we get into this debug tab what this breakpoint does is it breaks the execution of the program at this point so the second we hit this if statement we're no longer executing we're stopping and we're having a look at this statement so you can see on the right-hand side it shows me a bunch of variables and then it shows me these frames here so let's talk about the variables first so whenever you have a breakpoint when you hit that breakpoint it's gonna show you all of the different variables and so on within that scope and the scope right now is this main function so you say I have a variable run i have num num to result now look at my actual code it shows me with a little comment beside each variable what those variables are equal to and this is actually amazing I don't know how this even works but it can save you so much time especially if you're messing around and you accidentally have a variable that's resulting in none or something like that so you don't have to keep printing them to the screen it just shows you them right here so you can see ant equals 56 results six like this and it shows you the same thing here now I want to show you we can actually add new things to look at as well so say we want to just look at the variables that's great and this is gonna save us a lot of time but what if we want to check like an expression so we can actually use the sync old new watch so I'm going to click new watch and in here I'm simply just gonna type an expression in this case I want to say if string yeah NS loops is equal to and in this case we're gonna say what is it result like that okay and then I'm gonna click enter and you can see this gives us a result and it says false so it says if the string answer is equal to result and then it's just gonna give us the answer to that expression pretty much or whatever that evaluates to we can add as many as we want we could do something like an S + H and now you can see I get 56 H and you can do whatever you want here you can make them as long as you want and that's what you can look at different expressions without actually having to have them typed out in your code to delete one you can click the little minus key deletes it same thing here and add new ones again just click a new watch now I'm going to talk about these frames in just a second but let's go back to the console for one second so I what is three times 256 we've looked in our debug we've seen okay all this looks good we want to continue running our program now you can see we're still running we haven't stopped the program we just took a break at this breakpoint so to continue running the program we click this little icon here and it's just like the play button with a little line through it so resume program so the resume program and now we'll resume and we'll rerun everything until we hit the breakpoint again so we've run main again we've now printed another thing saying what is this value so it's 3 times 2 again so let's just type I don't know 5 ok now we've hit the breakpoint same thing happens we can go back to the debugger console we can see that ans equals 5 num num result and so on again if we want to run one more time we click run what is 3 times 2 it just because these aren't changing don't worry about that we do 6 ok if we hit the breakpoint same thing go back to debug and we can see all these values like that ok so when we want to stop the program we can just click stop debug and there we are now you can actually add multiple breakpoints as well so if you wanted to break first of all our results see what's happening there and then you want to resume and break at the else that's perfectly viable and you're allowed to do that ok so let's just add another breakpoint this time I want to add it at this random Trant int so whenever we call this I believe it's gonna break here okay so let's run the program again in debug mode and there we go right away we've hit this breakpoint that's because we're calling main we go num 1 equals generate random number and there we are so our upper limit in this function is equal to 10 and that's the only variable that we have so far so if we want to see the value of our well we would have to break at return our because we haven't actually yet set that variable because we haven't executed but I want to show you what these do up here so we have a step over step in step in to my code and step out and then step or return to run to cursor so if you're first looking at the easing might be like what the heck does this do doesn't really make any sense me well this is going to allow you to run line by line through your code and see exactly what's happening without adding a ton of different breakpoints so if I click step into like this you can see we actually open up random hi and this is the random module so import random and weird since we're calling a method on random and we've just clicked step into we're gonna step into this method and by doing that it needs to open up the function that is actually being culled and this is in this case it's rent int now this can be useful in some cases but in a long time but we don't want to step into other code so we don't want to step into like other methods or things that aren't actually a part of our own code so I'm gonna close this I'm gonna click resume and we're just we reach the breakpoint again because we're calling what do you call it numb to generate random number now what i'm gonna click is I'm gonna click step into my code and by stepping into my code what this does is it simply just goes to the next line so you can see that now instead of stepping into this rent in function we just step into the next line of my actual code so I'm gonna click it again step into my code and now you can see we return back to result and we can keep stepping through the program so step into my code if we want to step into this while loop we'll click step into my code and though now we get a and that's when we're gonna have to type something in the in the console now we can also do step over and what this does is simply gonna skip the line of code so we've stepped over and now we've actually stepped like completely over all of that code and now we're back into this main here because we just stepped over again so you kind of have to play with these and figure out how they work because it is kind of confusing but it's really useful so let's just add another breakpoint here at the if statement let's we're gonna break this and we're gonna run again we go to debugger and we've hit this oh it's because we have a breakpoint up here so let's just resume ok we just need to type something in the console so I'm gonna type 70 and now we hit this breakpoint if Anna study is digit okay so now we're gonna go and I click step over and you can see that it steps up to this next if statement I can step in words so into this if statement or it can step over so if I step over we go to correct keep going and I can keep stepping and we go back into this while loop like so so pretty much you're gonna have to play with these because they are hard to kind of explain how they all work but once you get the hang of these they'll save you a lot of time in debugging your code now there's one more thing I want to show so I'm gonna I just add another breakpoint in I don't know return R here okay so we're gonna run debug we're gonna be brought into here and you can see we have the variable R and upper now I want to show you this thing and it's called evaluate expression so it looks like a little calculator here if you click a value 8 expression you're actually able to evaluate any expression with the variables that are in the current scope so the ones that are showing up here so I do something like R plus 7 you can see I get a result of 17 I could do like string art like that and it gives me a result of 10 and you can evaluate any expressions you want without actually having to type them in your code again this will save you a lot of time and it's extremely useful if you don't want to be constantly printing things out and typing them in here because an idle to be able to do this you'd have to print string R and you'd have to print like the type of it whereas right here it gives you the exact value and even the type which is really useful so now I told you I was gonna talk about frames so let's do that here so in these frames we have multiple so we have a main frame which is well this function main we have module which is just anything that's within like the least indentation level so in our main line here so this stuff would be in the debug frame and we have generate random which is a frame for this function so we can check into multiple frames whenever we're kind of looking here so since we broke here before we were able to create anymore variables and create result if I step into the main debug frame here then it only gives me a variable of run because we haven't created an omni one yet we haven't created num2 yet we haven't created result so you can only see that one if I step in to generate random you can see that how the variable are in the variable upper and then same thing if I go back to the main one we have X in time so you can step through these frames and look at what's exactly happening in every function every method so on so again if I want to resume I'll resume and now if I go back to the main you can see that we have num1 is created because we've just stepped through another iteration until we hit this breakpoint so I'll do so again and you can see that our program just continues because now that we've called generate random twice we're never gonna hit this breakpoint again okay so I think that's been it kind of for debugging there is more things that you can do but this is kind of the basics and this will save you a lot of time if you can figure it out I recommend practicing how this debug thing works and kind of just playing around with it with some of your own code putting some breakpoints places seeing how it works cuz that's really the only way to understand all of it completely anyways that's been it for this video if you guys enjoyed please make sure you leave a like and subscribe and I will see you again in the next one [Music]
Info
Channel: Tech With Tim
Views: 239,230
Rating: undefined out of 5
Keywords: techwithtim, pycharm tutorial for beginners, pycharm tutorial, pycharm, how to use pycharm, pycharm guide, pycharm community edition tutorial, pycharm python tutorial, debugging in pycharm, debugging in pycharm python, pycharm debugger, python tutorial
Id: sRGpvbhOhQs
Channel Id: undefined
Length: 11min 46sec (706 seconds)
Published: Thu Nov 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.