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
Sam Altman : How to Build the Future
I’m Jack, Sam’s brother, and we are here in our backyard, where we also live with our other brother. Sam wanted to give some advice about how to have an impact on the world, and since you couldn’t interview him himself, here I am. So, Sam, thank you. Th…
"Where Love Is Illegal": Chronicling LGBT Stories of Love and Discrimination (Part 2) | Nat Geo Live
I was in Lagos, Nigeria in 2014 when I heard about five young men in the north of the country who faced the death penalty for committing gay acts. They were in the Sharia Law controlled part of the country. So I went up to see them. Fortunately, by the ti…
A warning about the Uber IPO…
What’s up, guys? It’s Graham here. So, two things. Number one, a picture of my land has been going viral lately on the internet. People are saying it’s like a black hole or something like that. I just want to put this to rest once and for all—it’s just a…
STOCK MARKET REACHES ANOTHER ALL TIME HIGH | DO THIS NOW
What’s up you guys? It’s Graham here. So, today has been a very eventful day. So eventful, in fact, that I had to replace the normal video I had scheduled today with this one because wow, today has been one of the best performing days for the stock market…
Making a Live Trap | Live Free or Die
Thorne’s girlfriend Delia’s counting on him to make sure they stay stocked up on meat, but he’s new to trapping and still doesn’t have a handle on the habits of all the wildlife in the area. “Now I’m gonna actually make a live trap. It’s kind of like a p…
Bullet Block Explained!
In my last video, we performed an experiment in which two identical wood blocks were shot with the same rifle, one through the center of mass and the other one slightly off to one side. Now, if you haven’t seen that video yet, then click here now and go a…