Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As you are building code in Python, you may occasionally make a typo, or misplace a punctuation mark or forget a comma or something like that. The Python interpreter will do its best to try to guide you to where the errors are and sometimes it's helpful and sometimes it's not. Let's take a look at some examples here. There is a file in your debugging folder called saytime-errors.py. We will go ahead and make a working copy of this and we will call this saytime-errors-working.py.
I will go ahead and open that working copy and this is the saytime module and it has some errors in it. So we will go ahead and we will run it and see what happens, and it's flagged a syntax error here. It says Syntaxerror: invalid syntax and you see that little caret is pointing at the second quote mark on line 26. So, we will scroll down here to line 26 and you see a little red x there and if I hold my cursor over the red x, we are getting a different error message and this is because Eclipse is doing its best to help too.
So Eclipse is looking at it from the perspective of syntax highlighting and Python is actually trying to run the code. So, looking at it from two different perspectives they are going to give you two different perspectives. You know they both might be helpful and they might not. We are going to focus on the Python error messages here, because you are not always going to be coding inside of Eclipse, and the syntax highlighting parser is not always going to be as helpful as the parser that's trying to run the code. So, in this case, it's pointing at this character here.
So, let's see if we can get the entire statement here on the screen and it starts here with _words = and it's got the opening curly brace and that ends down here with this closing curly brace. So the curly braces mean that this is a dictionary definition and you see there is our first key and a colon and there is a tuple object as the value and a comma. So, what we have here is a dictionary definition and we looked down here at this line with the error on it and we see there is colon there and there is the quote and there is the opening quote.
It's not really obvious that there is anything wrong with it. That quote does not look like a problem. Even though the syntax error is pointing at that quote, that quote does not look like a problem. So let's look at it in order. Let's go through the process of looking at this whole definition and see if we can figure out what's wrong. So this is the first key and then there is colon. The value is that whole tuple object and then there is a comma and then there is another key and a colon and then the value, which is another tuple object, and a comma.
So, we are seeing a pattern here. Key, value, comma, key, value, comma, we are getting closer, and here is a key and here is a value and there ought to be a comma there, because this looks like another key. So, we will put that comma in and we will save the file. We notice that the red x goes away. That's a good sign and we run it, and we get another error message. But this one is on line 70. So it looks like we have fixed this syntax error.
You see the little red mark over here that's probably line 70, sure is, and we have a different error message. This one says, IndentationError: unindent does not match any outer indentation level. So it says it's an indentation error and we see we have another dictionary definition and all of that looks okay. You see a little red squiggly mark from the syntax highlighter.
It's trying to help us there, but it's actually covering up the problem. You will notice that under those little red squiggly marks, there is an underscore and that underscore is indented farther out than the code above it, and so if we push that in one character and then save the file, see the red x goes away. Now we can see that underscore and we can see that indentation level. So that was an indentation error. You will notice that it's pointing at the wrong character here.
If it was pointing back here, it might have been more obvious. That's typical of parsers everywhere. They will do their best to show you where the error is and more often than not, they are off by a few characters and sometimes even a few lines of code. So go ahead and run in it again and now we have another error, and this one says it's on line 118 or line 129. We have two errors being printed out here and what this is doing actually, we fill the screen with this, this is what's called a traceback in Python.
It's telling where it encountered the error and it's tracing back its execution. So this is actually a runtime error. This is not syntax error. It says AttributeError, 'built_in_ function_or_method', object has no attribute 'tm_hour'. Here it is trying to assign this t.tm_ hour to a variable, self.hour, and that's at line 118. So, let's take a look at 118 and there is the line of code, and so what's happening is t here is an object that's getting passed into this constructor and saytime_t.
So what's the line above this? Scroll back down here and a line above this is print saytime_t and this is at line 129. So let's go down to 129 and there it is. And so it's passing this time.local time and I happen to know that local time there is a function. It is not an attribute. So it's correct here, object has no attribute, ?_hour, and that's because we didn't call it with the function. The function has the attribute.
So if we save this and run it again, now we get something else. This is not printed in red which means it's not being sent to standard error, which means it's not really a runtime error. This looks more like another kind of an error. It says bound method saytime. This is actually the output of this print statement here. It's printing a bound method, rather than running the bound method. So you see here .words. .words is supposed to be a method.
It's actually a method in one of our classes here, there it is. See it's a method here, def words, and so what we need to do, see just like the one up here. It needs to have parenthesis. So we will save that and we will run it and our script works as we expected. So that's pretty much the process that you have to go through. If you type a whole bunch of code and it's not working the way that you expect it to work, you need to have some patience. You need to look at carefully and objectively and sometimes from myself, if I am not seeing and I will go and do something else, clear my head and come back and look at it.
Usually at some point after I have cleared my head enough and I look at it enough, the errors will jump out and at me. These are the common type of syntax errors that you will run into in Python, things like forgetting parenthesis, indentation errors, forgetting commas, things like that. So you can start with looking for those sorts of things. Just have the patience, try and follow the code in your head, and eventually you ought to be able to find the errors and correct them.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 102146 Viewers
61 Video lessons · 88862 Viewers
71 Video lessons · 72679 Viewers
56 Video lessons · 104311 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.