debugging is just a fancy term for fixing errors in programs it's the process of removing bugs so we call it debug since it's something we'll be doing often let's learn how to work together with our IDE to track down and fix bugs in our programs here's a program I've been working on to calculate office expenses over the course of a month you'll notice that there's a linter air over here so that's probably already a good hint that I have a syntax error somewhere in my program but before I get too far ahead of myself let
me just hit run and see what happens all right suspicion confirmed the console says we have a syntax error remember that a syntax error means that our code is invalid python the first part of this message is what's called a stack trace a stack Trace traces or identifies the location in the program where the error originated the computer is telling us that the error is coming from a file called main.py as you can see over here this is the name of the file that we're working in this part then tells us that the error is
coming from line two of that file for our convenience it C line two into the stack Trace but we can also go over here in the code editor and look and see ah this is line two this little carrot in the stack Trace is pointing to this quotation mark character same thing with the linter it's kind of pointing toward the end of the line here the last line of the error message is the most important this is where the computer tells you what type of error it is in this case a syntax error and then
it makes its best guess at what the cause of the bug is now that guess is not always 100% correct you'll have to use your judgment here but it's usually a good starting place this error message says unterminated string literal naturally error messages tend to be worded in kind of obscure computer speak so let's try and translate what this means if you're not familiar with a particular error sometimes the easiest thing to do is copy and paste that last line of the error message into a search engine and see what other people say okay so
I'm looking up unterminated string literal Python and I see here someone explain it as when you forget to close the string with a matching quote ah okay that makes sense this little carrot here is pointing at this quotation mark character and that's next to another quotation mark character and so why do I have two I have mismatch quotation mark So if I just delete this then I have a string and then let me try running it again boom okay that error message went away and so did the linter error but now I have a new
error let's try debugging this one now this error message just says invalid syntax so that means the computer doesn't really have a best guess at what's going on it says perhaps I forgot a comma I don't really understand why would I want a comma here I don't know let me try putting one in and running it that still seems wrong okay I'm going to undo that I'm a little stumped on this one and the computer's not really offering much help so one strategy I can use is to comment this line out we know that the
computer doesn't execute lines of code that are commons I don't want to just delete this line because I do want to fix it at some point but maybe I'll come back to it later so what I can do is stick a commment character this little hashtag in front of this line to turn it into a comment and that means when the computer runs the program it'll now ignore that line of code okay now that I commented out that line the lter error jumps to line 14 the lint message says unexpected token indent hm okay that
probably means there's something going wrong with my indentation let me just run the program and make sure great okay the error message agrees there's an indentation error on line 14 ah okay there's a space here well that's what the little red squiggly the linter is underlining and there shouldn't be everything needs to be lined up with the left margin so let me just delete the space real quick now I'm seeing a runtime error I know it's a runtime error because the output from the first few print functions is showing up in the console that means
that the computer started running my program cuz it ran these first few lines and it only got stuck when it got to line seven this is another one where I'm not really sure what this error message means so I'm going to try looking it up again if I browse around for a bit I'm still not really understanding what people are saying is wrong maybe I just don't have the necessary background context yet let me try a different approach then I'm going to take a step back and think about what I wanted this line of code
to do based on the comment above it seems like I'm trying to add the price of the birthday cake and the price of the keyboard together hm okay then I should be adding 34 + 22.44% but I did comment out that one line before so let me go back and try and figure that syntax error out hm okay now that I'm looking at it again I noticed that there's something going on with the syntax highlighting week 2 expenses is black and it should be green cuz it should be a string right cuz I'm trying to
print out a string ooh okay I need quotation marks around this thing amazing okay I think I have fixed all of the syntax errors and runtime errors am I done debugging nope I haven't checked for logic errors yet let me step through this output and make sure that the results all make sense to me okay I'm printing out a heading for the report then I have miscellaneous expenses 5644 that seems within reason week one expenses that number seems within reason week two expenses why are there three decimal places here that's not a legal price value
let me go to the line of the program that's calculating that sum ooh interesting I have the price 18.42 here what is that number supposed to be looking back at the utility bill for that week it looks like that number is supposed to be $842 so let me just fix that cool let's continue checking the rest of the output for logic errors we have week three expenses and that number looks good and that's it my program finally works next time though I should probably remember to run my program as I go so I never end
up with that many errors all at once happy debugging