Premium Only Content
Runtime Errors |Section 2|Celestial Warrior
1
00:00:01,429 --> 00:00:06,240
Hi again and welcome to this new lecture!
In the previous lecture we talked about
3
00:00:06,240 --> 00:00:13,320
errors in general and I focused on
syntax errors which is one of the two
5
00:00:13,320 --> 00:00:17,250
types of errors that you have in Python.
So you have syntax errors and you have
7
00:00:17,250 --> 00:00:23,640
exceptions. Now we're going to talk about
exceptions in Python, so every other type
9
00:00:23,640 --> 00:00:29,699
of error that is not a syntax error is
an exception. And often all errors,
11
00:00:29,699 --> 00:00:35,160
syntax errors and exceptions they are
referred to as errors so you're going to
13
00:00:35,160 --> 00:00:41,219
hear this all over the place. Now I have
a new script here that I created for
15
00:00:41,219 --> 00:00:46,050
this lecture.
Try to guess what I'm going to get as
17
00:00:46,050 --> 00:00:53,340
output when I execute this script, so I
have a equal to 1, n equals to characters 2.
19
00:00:53,340 --> 00:01:04,430
Print int 2.5 and print a plus b. So
guess what I'm going to get for a moment.
21
00:01:04,430 --> 00:01:17,369
Okay, now let me run the script Python 3.
Errors.py. File errors.py line 4
23
00:01:17,369 --> 00:01:28,020
which is the last line, this one. We get
a syntax error, invalid syntax. Now this
25
00:01:28,020 --> 00:01:31,979
can be very confusing for you now
because you know you're looking at the
27
00:01:31,979 --> 00:01:40,680
print a plus b expression, but you don't
see any error there. So for a beginner of
29
00:01:40,680 --> 00:01:49,020
this can be quite frustrating but try to
look back on the left of arrow here, so
31
00:01:49,020 --> 00:01:54,509
think of the arrow that the arrow is
pointing at the token and token has not
33
00:01:54,509 --> 00:01:59,670
been written correctly in the script, so
in this case you know the problem
35
00:01:59,670 --> 00:02:07,600
here is that this line here it has an
open bracket, round bracket
37
00:02:07,600 --> 00:02:13,920
and than it has an int function, and then the int
function has its own brackets that is
39
00:02:13,920 --> 00:02:19,360
wrapping inside its input, so these are
the brackets of the int function, but the
41
00:02:19,360 --> 00:02:24,640
print function doesn't have a closing
bracket. What you did instead, what I did
43
00:02:24,640 --> 00:02:30,760
actually, what I did is that instead of
putting a bracket there I actually wrote
45
00:02:30,760 --> 00:02:38,050
another print function so Python was
expecting a closing round
47
00:02:38,050 --> 00:02:43,450
bracket, but I wrote I typed in a print
function. That's why this is saying that
49
00:02:43,450 --> 00:02:49,420
this print function is not in the
correct position. That's the idea.
51
00:02:49,420 --> 00:02:55,030
So always when you see this arrow first
look at this line event look before that
53
00:02:55,030 --> 00:03:03,940
also. Sorry about this! So always when you
see this arrow here, first look at this
55
00:03:03,940 --> 00:03:09,610
line here but also don't forget that the
problem might also be before this
57
00:03:09,610 --> 00:03:14,340
line.
Okay, so that was about the syntax error.
59
00:03:14,340 --> 00:03:19,299
I'll explain why I talked about this
syntax error here let me execute this
61
00:03:19,299 --> 00:03:24,850
again. So the syntax error was fixed now,
but we still have another error. So the
63
00:03:24,850 --> 00:03:29,650
reason I included a syntax error in my
code was to show you that Python
65
00:03:29,650 --> 00:03:36,190
actually first checks for syntax errors
so basically it parses the code, it looks
67
00:03:36,190 --> 00:03:43,060
for syntax errors, it doesn't execute the
code yet. So when I executed Python 3
69
00:03:43,060 --> 00:03:47,170
errors that's why i in here the code was
not executed but the interpreter just
71
00:03:47,170 --> 00:03:53,950
checks for syntax errors, it doesn't
check for exceptions just yet. So first
73
00:03:53,950 --> 00:03:56,890
you need to fix the syntax errors and
that's what we did here. We added
75
00:03:56,890 --> 00:04:02,610
these brackets and we fixed the syntax errors.
Now Python is throwing out an exception and
77
00:04:02,610 --> 00:04:08,620
let's, here is where I executed the code
and see the next line we have number two
79
00:04:08,620 --> 00:04:14,380
there which is coming from the output of
this line here, so from line three we
81
00:04:14,380 --> 00:04:19,239
got the output correctly. So basically
again Python executes the script from top
83
00:04:19,239 --> 00:04:22,810
to bottom
if it doesn't find
85
00:04:22,810 --> 00:04:28,930
syntax errors. If it finds syntax errors
it doesn't execute anything. So 2 is
87
00:04:28,930 --> 00:04:36,360
printed out and then you get this trace
back of the error which starts there and
89
00:04:36,360 --> 00:04:44,680
ends in here that's a block of the error.
Sometimes the errors may have more than
91
00:04:44,680 --> 00:04:50,950
one type of error. So in this case
we have only one type of error which is
93
00:04:50,950 --> 00:04:56,830
a type error, but you may have multiple
blocks here. However the most important
95
00:04:56,830 --> 00:05:02,710
error that you must focus on is the lost
line of the error, so the last block of
97
00:05:02,710 --> 00:05:08,520
of the error. In this case we have only
one block so we're focused on that.
99
00:05:08,520 --> 00:05:16,570
And line 4 which is this one in here. This
is the line. Again the line is printing out
101
00:05:16,570 --> 00:05:21,520
just like in the case of syntax error.
And you have a type of the error here
103
00:05:21,520 --> 00:05:26,920
which is a type error. So what's a type
error? A type error means that there is
105
00:05:26,920 --> 00:05:35,530
something wrong with one of your object
types in your script, and you have the
107
00:05:35,530 --> 00:05:42,220
description here. Unsupported operand
type for plus so this is trying to say
109
00:05:42,220 --> 00:05:48,130
that the plus operator has an
unsupported type so the plus operator in
111
00:05:48,130 --> 00:05:52,690
other words doesn't support one of the
types that you have given to it, so it's
113
00:05:52,690 --> 00:05:59,730
either the type of variable a or the
type of variable b. So it says int and
115
00:05:59,730 --> 00:06:06,730
string, so it doesn't specifically say
whether int or string is an object but
117
00:06:06,730 --> 00:06:12,250
it says that you cannot use a plus
operator with an integer and a string.
119
00:06:12,250 --> 00:06:18,670
And that's logically wrong
because you cannot add a number to some
121
00:06:18,670 --> 00:06:24,150
text. That's what Python does not
understand so it throws a type error.
123
00:06:24,150 --> 00:06:30,500
So exceptions are like logical errors
and you need to use your logic now to
125
00:06:30,500 --> 00:06:35,720
fix the error. And you use your logic by
carefully inspecting the error and
127
00:06:35,720 --> 00:06:40,790
that's what we did.
So what we want to do, we want to fix
129
00:06:40,790 --> 00:06:47,390
this. Now it's up to me whether I
meant to concatenate these two objects
131
00:06:47,390 --> 00:06:54,800
or do a mathematical addition operation.
So let's say if I was intending to
133
00:06:54,800 --> 00:07:01,190
do an addition between these two numbers
then I would have to convert b to a float
135
00:07:01,190 --> 00:07:09,710
or an integer.
Save that, execute, and yeah in this case you
137
00:07:09,710 --> 00:07:14,840
don't get any error. You get 2 printed
out from the second, from the third line
139
00:07:14,840 --> 00:07:22,310
and 3.0 from the last line that we have
just fixed. However if my intention was
141
00:07:22,310 --> 00:07:29,690
to actually print out the concatenation
between these two strings, instead of
143
00:07:29,690 --> 00:07:36,310
converting b to a float, I would have to
convert a to a string.
145
00:07:36,310 --> 00:07:42,980
In this case I would get the text
12. 12, so that would be
147
00:07:42,980 --> 00:07:50,060
a string object, not a number. Even though
here it shows as a number it's just how
149
00:07:50,060 --> 00:07:57,169
the terminal prints it out. So again these
are errors that occur in runtime, so when
151
00:07:57,169 --> 00:08:03,110
the script executes. Syntax errors are
parsing errors so the interpreter tries
153
00:08:03,110 --> 00:08:07,640
to understand whether the script is
syntactically correct, whether
155
00:08:07,640 --> 00:08:13,910
you have followed the Python
syntax rules. So you need a bracket,
157
00:08:13,910 --> 00:08:18,560
a closing bracket for an opening bracket.
You need a closing quote after the opening
159
00:08:18,560 --> 00:08:26,570
quote and so on. Now there are also
other types of exceptions, not only type
161
00:08:26,570 --> 00:08:34,030
error. You know you may have a name error.
Let's say here instead of printing out
163
00:08:34,030 --> 00:08:43,330
that, we print out c.
Save, execute and here is the trace back.
165
00:08:43,330 --> 00:08:50,970
Again 2 was printed out from the third line
and the trace back says line 4 at
167
00:08:50,970 --> 00:09:00,730
print c, name error name c is not
defined. So again this is not a syntax
169
00:09:00,730 --> 00:09:04,750
error because you haven't made any
mistakes with the syntax. You know you have
171
00:09:04,750 --> 00:09:10,120
this variable name which is correct,
you have brackets opening, round brackets
173
00:09:10,120 --> 00:09:14,890
and closing round bracket, so everything
is syntactically correct both this c
175
00:09:14,890 --> 00:09:20,170
object Python doesn't know this. You
haven't defined this c variable so
177
00:09:20,170 --> 00:09:23,920
Python doesn't know what to print out.
You know python is able to print out a
179
00:09:23,920 --> 00:09:30,940
because it knows that a refers to the
1 to integer 1 and so it prints out
181
00:09:30,940 --> 00:09:36,790
integer 1, but c doesn't have anything
so you get this name error and whenever
183
00:09:36,790 --> 00:09:42,520
you get a name error you know that this
name here has not been defined by you so
185
00:09:42,520 --> 00:09:48,490
to fix this you may want to define c
like that and execute and you don't get
187
00:09:48,490 --> 00:09:54,160
any error. You may also have other
types of errors such as c divided by 0.
189
00:09:54,160 --> 00:10:02,080
See what you get. Zero division
error division by 0. That's the
191
00:10:02,080 --> 00:10:07,300
description of this error so division by
0 is not mathematically possible or
193
00:10:07,300 --> 00:10:13,330
meaningful and since python is based on
mathematics it throws an error in
195
00:10:13,330 --> 00:10:17,200
and you need to fix that. You need to
remove that expression that divides by 0.
197
00:10:17,200 --> 00:10:25,720
Yeah, that's about errors in Python.
I hope you understood a great deal of this
199
00:10:25,720 --> 00:10:30,820
lecture and the previous lecture as well.
So it's relatively easy to fix errors,
201
00:10:30,820 --> 00:10:35,800
however sometimes
there are errors that you might have
203
00:10:35,800 --> 00:10:41,620
difficulties understanding and fixing
them so later for example we're going to
205
00:10:41,620 --> 00:10:45,220
work on libraries and sometimes
libraries they have different kinds of
207
00:10:45,220 --> 00:10:49,900
errors, however nothing to worry about
because there are other things that you
209
00:10:49,900 --> 00:10:53,050
can do
for an arrow that you don't understand.
211
00:10:53,050 --> 00:10:58,000
And I'll talk about that in the next
lecture, so I'll see you there!
-
1:45:59
Spittin' Chiclets
16 hours agoCanadian Chokejob - Game Notes Live From Chicago - 12.28.2024
60.3K6 -
FusedAegisTV
5 hours agoWelcome to The King of Iron Fist Tournament! \\ TEKKEN 8 Stream #1
47K -
DVR
Bannons War Room
1 year agoWarRoom Live
101M -
5:42:36
FreshandFit
10 hours agoLive X Censorship For Opposing Immigration?!
99.5K71 -
1:08:16
Tactical Advisor
6 hours agoNEW Budget Glocks | Vault Room Live Stream 011
34.7K3 -
16:30
SNEAKO
13 hours agoNO FRIENDS IN THE INDUSTRY.
76.5K18 -
6:19
BlackDiamondGunsandGear
1 day agoHow Fat Guys can Appendix Carry
48.5K4 -
6:58
Gun Owners Of America
1 day ago2024 Was Huge For Gun Rights, Here's Our Top 10 Wins!
42.6K2 -
15:50
Degenerate Jay
1 day ago $1.73 earnedJames Bond Is Being Ruined By Amazon? Make Him A Black Gay Woman?
31.7K9 -
15:18
DeVory Darkins
1 day ago $15.01 earnedTrump Drops NIGHTMARE Warning on Joe Biden
47.1K95