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
HONEST TRUTH About Creating A SUCCESSFUL BUSINESS & Why MOST FAIL! | Kevin O'Leary
People bs themselves. They say to themselves, “I’m going to game the system; I’m going to tell everybody that if they buy a pair of socks from me, I’ll give a pair to charity; I’ll get lots of free press. The buyers at Walmart will want to see me because …
Hunting and Eating Invasive Iguanas | National Geographic
They’re invasive species in Puerto Rico, and we’re trying to control that problem so we could start eating them and we can start hunting them. El grupo de loja, one arrowed, agua de este tzedakah, pay a day’s end. L agree cultura de Puerto Rico pro Pokag…
Iceland’s Glaciers - 360 | Into Water
Glaciers are natural wonders. They’re shapeshifters, wild and alive. They hold the keys to the secrets of humanity’s past and humanity’s future. I’m Dr. M. Jackson. I am a geographer, a climatologist, and a National Geographic Explorer. For over a thousa…
How To Become A Metaverse Millionaire In ONE Year (My Plan)
The best way to understand the metaverse is to experience it yourself. Alright, so let’s talk about the metaverse because this could be big, not only as a way to make a lot of money but also in terms of how all of our future experiences could be right her…
15 Life-Changing Lessons We Learned in 2023
A man who does not reflect on the year that’s passed is destined to repeat it. With this year coming to a close, we make a priority of externalizing the most valuable insights we’ve drawn, and we’re about to share them with you. Here are 15 valuable lesso…
Charlie Munger: "I Got Rich When I Understood This" (Mental Models)
Billionaire investor Charlie Munger has said on countless occasions he got rich when he finally understood the power of what I referred to as mental models. I have gone through hundreds of hours of Charlie Munger’s interviews and writings to identify the …