yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Debugging with stack traces | Intro to CS - Python | Khan Academy


5m read
·Nov 10, 2024

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 error 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 cites 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 caret 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 explaining it as when you forget to close the string with a matching quote. Ah, okay, that makes sense. This little caret here is pointing at this quotation mark character, and that's next to another quotation mark character. So why do I have two? I have mismatched quotation marks. 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 I would 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 comments. 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 comment 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 linter error jumps to line 14. The lint message says "unexpected token indent." Hmm, 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 because 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. Hmm, 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.

Hmm, 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, 'cause it should be a string, right? 'Cause 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, $564.44. 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!

More Articles

View All
Fishing in the Yukon River | Life Below Zero
That’s a bourbon! Holy look, Maya! I got it! Yeah, you got it! Maya was able to pull out a lush, which was a big deal because it’s a different kind of fish. None of my kids ever seen one; I’ve never caught one, and I was really proud of her to be able to …
SMARTER EVERY DAY AND SPACE!!!! - 129
Hey, it’s me Destin, welcome back to Smarter Every Day. So of everything I’ve studied on Smarter Every Day, if you know anything about my educational background or my family history, you know that space is this holy topic. It’s something that must be appr…
Magnetic forces | Forces at a distance | Middle school physics | Khan Academy
Let’s talk about magnets and magnetic forces. Magnets are these neat objects that are able to attract metals like iron. Magnets are used in all sorts of things, from holding paper on your refrigerator to computers to compasses. So, magnets can be used to …
2015 AP Chemistry free response 2 d e
The Lewis electron dot diagram for C2H4 is shown below. In the box on the left, in the box on the right, complete the Lewis electron dot diagram for C2H5O, or ethanol, by drawing in all of the electron pairs. As they said, this right over here, this is t…
Zero-order reactions | Kinetics | AP Chemistry | Khan Academy
Let’s say we have a hypothetical reaction where reactant A turns into products. Let’s say the reaction is zero order with respect to A. If it’s zero order with respect to A, we can write that the rate of the reaction is equal to the rate constant k times …
$0 DOWN MORTGAGES ARE BACK (Get Paid To Buy A Home)
What’s up you guys? It’s Graham here, and the housing market is about to explode. That’s right! In the middle of record-high prices, record-high mortgage rates, and record-low inventory, a brand new proposal was just announced that would give first-time h…