1
00:00:00,000 --> 00:00:04,990
As you are building code in Python, you may occasionally make a typo, or

2
00:00:05,000 --> 00:00:08,990
misplace a punctuation mark or forget a comma or something like that.

3
00:00:09,000 --> 00:00:13,990
The Python interpreter will do its best to try to guide you to where the errors

4
00:00:14,000 --> 00:00:17,990
are and sometimes it's helpful and sometimes it's not.

5
00:00:18,000 --> 00:00:19,990
Let's take a look at some examples here.

6
00:00:20,000 --> 00:00:25,990
There is a file in your debugging folder called saytime-errors.py. We will

7
00:00:26,000 --> 00:00:29,990
go ahead and make a working copy of this and we will call this

8
00:00:30,000 --> 00:00:34,990
saytime-errors-working.py.

9
00:00:35,000 --> 00:00:40,990
I will go ahead and open that working copy and this is the saytime module and it

10
00:00:41,000 --> 00:00:42,990
has some errors in it.

11
00:00:43,000 --> 00:00:47,990
So we will go ahead and we will run it and see what happens, and it's flagged

12
00:00:48,000 --> 00:00:49,990
a syntax error here.

13
00:00:50,000 --> 00:00:50,990
It says Syntaxerror:

14
00:00:51,000 --> 00:00:55,990
invalid syntax and you see that little caret is pointing at the second

15
00:00:56,000 --> 00:00:57,990
quote mark on line 26.

16
00:00:58,000 --> 00:01:07,990
So, we will scroll down here to line 26 and you see a little red x there and if

17
00:01:08,000 --> 00:01:12,990
I hold my cursor over the red x, we are getting a different error message and

18
00:01:13,000 --> 00:01:16,990
this is because Eclipse is doing its best to help too.

19
00:01:17,000 --> 00:01:22,990
So Eclipse is looking at it from the perspective of syntax highlighting and

20
00:01:23,000 --> 00:01:25,990
Python is actually trying to run the code.

21
00:01:26,000 --> 00:01:27,990
So, looking at it from two different perspectives they are going to give you two

22
00:01:28,000 --> 00:01:29,990
different perspectives.

23
00:01:30,000 --> 00:01:31,990
You know they both might be helpful and they might not.

24
00:01:32,000 --> 00:01:35,990
We are going to focus on the Python error messages here, because you are not

25
00:01:36,000 --> 00:01:39,990
always going to be coding inside of Eclipse, and the syntax highlighting

26
00:01:40,000 --> 00:01:44,990
parser is not always going to be as helpful as the parser that's trying to run the code.

27
00:01:45,000 --> 00:01:49,990
So, in this case, it's pointing at this character here.

28
00:01:50,000 --> 00:01:53,990
So, let's see if we can get the entire statement here on the screen and it

29
00:01:54,000 --> 00:02:00,990
starts here with _words = and it's got the opening curly brace and that ends

30
00:02:01,000 --> 00:02:02,990
down here with this closing curly brace.

31
00:02:03,000 --> 00:02:07,990
So the curly braces mean that this is a dictionary definition and you see

32
00:02:08,000 --> 00:02:16,990
there is our first key and a colon and there is a tuple object as the value and a comma.

33
00:02:17,000 --> 00:02:21,990
So, what we have here is a dictionary definition and we looked down here at this

34
00:02:22,000 --> 00:02:27,990
line with the error on it and we see there is colon there and there is the quote

35
00:02:28,000 --> 00:02:28,990
and there is the opening quote.

36
00:02:29,000 --> 00:02:31,990
It's not really obvious that there is anything wrong with it.

37
00:02:32,000 --> 00:02:33,990
That quote does not look like a problem.

38
00:02:34,000 --> 00:02:38,990
Even though the syntax error is pointing at that quote, that quote does not

39
00:02:39,000 --> 00:02:39,990
look like a problem.

40
00:02:40,000 --> 00:02:45,990
So let's look at it in order. Let's go through the process of looking at this

41
00:02:46,000 --> 00:02:48,990
whole definition and see if we can figure out what's wrong.

42
00:02:49,000 --> 00:02:53,990
So this is the first key and then there is colon. The value is that whole tuple

43
00:02:54,000 --> 00:02:59,990
object and then there is a comma and then there is another key and a colon and

44
00:03:00,000 --> 00:03:02,990
then the value, which is another tuple object, and a comma.

45
00:03:03,000 --> 00:03:04,990
So, we are seeing a pattern here.

46
00:03:05,000 --> 00:03:13,990
Key, value, comma, key, value, comma, we are getting closer, and here is a key

47
00:03:14,000 --> 00:03:20,990
and here is a value and there ought to be a comma there, because this looks like another key.

48
00:03:21,000 --> 00:03:24,990
So, we will put that comma in and we will save the file.

49
00:03:25,000 --> 00:03:25,990
We notice that the red x goes away.

50
00:03:26,000 --> 00:03:30,990
That's a good sign and we run it, and we get another error message.

51
00:03:31,000 --> 00:03:32,990
But this one is on line 70.

52
00:03:33,000 --> 00:03:36,990
So it looks like we have fixed this syntax error.

53
00:03:37,000 --> 00:03:43,990
You see the little red mark over here that's probably line 70, sure is, and we

54
00:03:44,000 --> 00:03:45,990
have a different error message.

55
00:03:46,000 --> 00:03:48,990
This one says, IndentationError:

56
00:03:49,000 --> 00:03:52,990
unindent does not match any outer indentation level.

57
00:03:53,000 --> 00:03:58,990
So it says it's an indentation error and we see we have another dictionary

58
00:03:59,000 --> 00:04:02,990
definition and all of that looks okay.

59
00:04:03,000 --> 00:04:06,990
You see a little red squiggly mark from the syntax highlighter.

60
00:04:07,000 --> 00:04:11,990
It's trying to help us there, but it's actually covering up the problem.

61
00:04:12,000 --> 00:04:14,990
You will notice that under those little red squiggly marks, there is an

62
00:04:15,000 --> 00:04:19,990
underscore and that underscore is indented farther out than the code above it,

63
00:04:20,000 --> 00:04:27,990
and so if we push that in one character and then save the file, see the red x goes away.

64
00:04:28,000 --> 00:04:30,990
Now we can see that underscore and we can see that indentation level.

65
00:04:31,000 --> 00:04:32,990
So that was an indentation error.

66
00:04:33,000 --> 00:04:37,990
You will notice that it's pointing at the wrong character here.

67
00:04:38,000 --> 00:04:40,990
If it was pointing back here, it might have been more obvious.

68
00:04:41,000 --> 00:04:42,990
That's typical of parsers everywhere.

69
00:04:43,000 --> 00:04:47,990
They will do their best to show you where the error is and more often than not,

70
00:04:48,000 --> 00:04:51,990
they are off by a few characters and sometimes even a few lines of code.

71
00:04:52,000 --> 00:04:57,990
So go ahead and run in it again and now we have another error, and this one says

72
00:04:58,000 --> 00:05:01,990
it's on line 118 or line 129.

73
00:05:02,000 --> 00:05:08,990
We have two errors being printed out here and what this is doing actually, we fill

74
00:05:09,000 --> 00:05:14,990
the screen with this, this is what's called a traceback in Python.

75
00:05:15,000 --> 00:05:18,990
It's telling where it encountered the error and it's tracing back its execution.

76
00:05:19,000 --> 00:05:20,990
So this is actually a runtime error.

77
00:05:21,000 --> 00:05:22,990
This is not syntax error.

78
00:05:23,000 --> 00:05:27,990
It says AttributeError, 'built_in_ function_or_method', object has no

79
00:05:28,000 --> 00:05:29,990
attribute 'tm_hour'.

80
00:05:30,000 --> 00:05:40,990
Here it is trying to assign this t.tm_ hour to a variable, self.hour, and that's at line 118.

81
00:05:41,000 --> 00:05:48,990
So, let's take a look at 118 and there is the line of code, and so what's

82
00:05:49,000 --> 00:05:55,990
happening is t here is an object that's getting passed into this constructor and saytime_t.

83
00:05:56,000 --> 00:05:58,990
So what's the line above this?

84
00:05:59,000 --> 00:06:05,990
Scroll back down here and a line above this is print saytime_t and this is at line 129.

85
00:06:06,000 --> 00:06:14,990
So let's go down to 129 and there it is. And so it's passing this time.local time

86
00:06:15,000 --> 00:06:19,990
and I happen to know that local time there is a function.

87
00:06:20,000 --> 00:06:22,990
It is not an attribute.

88
00:06:23,000 --> 00:06:28,990
So it's correct here, object has no attribute, ?_hour, and that's because we

89
00:06:29,000 --> 00:06:32,990
didn't call it with the function. The function has the attribute.

90
00:06:33,000 --> 00:06:37,990
So if we save this and run it again, now we get something else. This is not

91
00:06:38,000 --> 00:06:41,990
printed in red which means it's not being sent to standard error, which means

92
00:06:42,000 --> 00:06:43,990
it's not really a runtime error.

93
00:06:44,000 --> 00:06:46,990
This looks more like another kind of an error.

94
00:06:47,000 --> 00:06:48,990
It says bound method saytime.

95
00:06:49,000 --> 00:06:51,990
This is actually the output of this print statement here.

96
00:06:52,000 --> 00:06:59,990
It's printing a bound method, rather than running the bound method.

97
00:07:00,000 --> 00:07:01,990
So you see here .words.

98
00:07:02,000 --> 00:07:05,990
.words is supposed to be a method.

99
00:07:06,000 --> 00:07:11,990
It's actually a method in one of our classes here, there it is.

100
00:07:12,000 --> 00:07:21,990
See it's a method here, def words, and so what we need to do, see just like the one up here.

101
00:07:22,000 --> 00:07:21,990
It needs to have parenthesis.

102
00:07:22,000 --> 00:07:29,990
So we will save that and we will run it and our script works as we expected.

103
00:07:30,000 --> 00:07:32,990
So that's pretty much the process that you have to go through.

104
00:07:33,000 --> 00:07:36,990
If you type a whole bunch of code and it's not working the way that you expect

105
00:07:37,000 --> 00:07:40,990
it to work, you need to have some patience. You need to look at carefully and

106
00:07:41,000 --> 00:07:46,990
objectively and sometimes from myself, if I am not seeing and I will go and do

107
00:07:47,000 --> 00:07:50,990
something else, clear my head and come back and look at it.

108
00:07:51,000 --> 00:07:54,990
Usually at some point after I have cleared my head enough and I look at it

109
00:07:55,000 --> 00:07:56,990
enough, the errors will jump out and at me.

110
00:07:57,000 --> 00:08:00,990
These are the common type of syntax errors that you will run into in Python,

111
00:08:01,000 --> 00:08:05,990
things like forgetting parenthesis, indentation errors, forgetting commas, things like that.

112
00:08:06,000 --> 00:08:09,990
So you can start with looking for those sorts of things.

113
00:08:10,000 --> 00:08:15,990
Just have the patience, try and follow the code in your head, and eventually you

114
00:08:16,000 --> 00:08:26,000
ought to be able to find the errors and correct them.

