 In this video, we're going to look at common error messages that you may see while programming. And also, we're going to introduce a classification scheme of three different types of errors. And then for each of them, we will see what are common solution strategies to fix the error messages. So let's first create a new file and call it eros.ipython notebook. And start with an easy example. So we have seen how we can add numbers in Python. So for example, something that Python understands is if you write 1 plus 2 or 1 plus 1, then Python simply adds the two numbers together. However, suppose you want to model accounting data or data that comes from some financial source. And let's say you want to add, let's say, 399 US dollars to, let's say, 999 US dollars. If we execute this cell, we get an error message. And this is what the red background here indicates. And in Python, it is such that every error that occurs always has a given error type. And the error type is usually what is written first here in the error message. So here it is a syntax error. And then you often get a message that further describes what the error message means or what is going wrong. For the syntax error, it basically, Python basically says, well, the syntax is invalid. And then it's basically Python telling us that it does not understand what we want. And the reason here is because Python cannot understand the dollar symbol. So Python knows many different symbols. And some of them, many of them are actually operators just like the plus symbol itself. But the dollar symbol, it just doesn't know. It's not programmed into the Python language. And whenever Python sees a symbol it doesn't know how to deal with, it simply raises a syntax error and it indicates to us which symbol it doesn't understand. And now the syntax error can also occur in a different scenario. So for example, remember from a previous video where we wanted to find the average of all the even numbers in the list and we used an approach where we used a for loop. So let's write a for loop here. So let's first go ahead and write for a number in and now instead of a variable, let's say numbers that we used in a previous video, we will now create a list object on the fly and we will say one, two, three here. And you know that for a for loop to work, the first line that has the four in it has to end with a colon. So let's end with a colon. And then in the next line, let's simply say print number squared, number to the power of two. And now just to illustrate a point, let's also remove the colon here. Let's just pretend that we forget it and let's execute this code cell and we also get a syntax error. And so here the error is at the end and basically the error pointing at an empty space basically tells us something is missing here. Okay, so these are two different kinds of situation where we will see in syntax error. Okay, and the fix here is obvious, right? It is just to put the colon back, but we will leave it like this. Another error of the same category, but this time a different type of error is the following. So let's go ahead and it's right for number in records one, two, and three. This time we don't forget the colon. And now we go ahead and instead of indenting by four spaces, we will simply say print number squared. And now we get an intendation error. So Python says expecting an indented block. Okay, so a block is a group of lines of code. And then that means we have to put at least one space here. Then this would make the code cell work. However, the best practice is to use multiples of four spaces per level of intendation. And then the code works. However, if we, as we just saw, leave out all the intendation, then Python will give us back an intendation error. So now we have two different types of errors, a syntax error and an intendation error. However, they both belong to the same classification. And the classification I introduced here is also called a syntax error. And the characteristic property of any syntax error is Python does not understand what our code means. Okay, it cannot read it. And the thing, and that may sound counter-intuitive to you, the thing that is very good here is that this type of error, these types of error, they will always occur. So these errors will always, and that is important. So let's maybe emphasize this. These errors will always show up. So when we make them, then the program will never, ever run. So that is a good property to have, because if we have an error and we see it, we can fix it and after we fix it, the error is gone. And that solves the problem. So let's say this is good as we can fix the error. Okay, so note how one of the three categories for classifying errors that I'm introducing, one of them is the syntax error. And so syntax error here is a classification. And syntax error down here is the type of a specific error and the indentation error is also classified in the category syntax errors. Okay, there's a double meaning here. Okay, now let's look at a different class of errors. And these are so-called runtime errors. So runtime errors have the property that the code is syntactically correct. It could work, but given the specific data of the problem, the code does not run. So an example for that is the following. Let's go ahead and let's divide the number one by two. And this of course works. It gives us back 0.5. However, if you divide the number one by zero, then in math, we have learned that this shouldn't work. And also in programming, this does not work. It gives us a zero division error. So here, the characteristic property is that Python knows how to read this. Up here, Python does not know how to read the code. Here it knows how to read the code. It understands, okay, there are two numbers. And I should divide the first by the second. However, if the second number is a zero, then we cannot divide by zero. So what we do here is, let's write here, divide by zero. And the characteristic here in a real world program is the error occurs only sometimes. But that is the characteristic. Usually if you write a program that has a runtime error, then the program will run for most of the time. But every once in a while, it doesn't run. And other examples, so let's maybe put that here, examples. Bad data from an external source. So let's assume we load in this data from a CSV file and we assume that in every row in a specific column in a CSV file, there is a number. And then either for some specific row, there is no number. There's a missing value or maybe there is a zero and we want to divide by the column. Then the code would technically work, but given the specific file, the specific data in the CSV file, the code does not work. Okay, and there is syntax to handle this. Okay, so this is still good as Python offers special syntax to deal with that. And I'm not going to talk about this here. I will just give you one brief word where you can look something up. Python has what is called a try statement. And we will look into the try statement in a future video. But using the try statement, you can try out if a code runs. And if it doesn't run, you can react to this situation. Okay, and so, but that's the characteristic part here, right? We could actually do something with it. And we see the error only a couple of times. And in the majority of the cases, the code would run. And now comes a third category of errors. And we call that so called semantic errors. Okay, so what are semantic errors? The semantic errors never occur in a program. Let me rephrase that. Let's say semantic errors, they actually always occur in a program, but they never go up. So let's say the red error message that we have seen above. This we will never see for semantic error. Okay, and that is very, very bad because we often do not. And let's use the emphasis. We often do not know that the arrow exists. And there's only one way to deal with that in practice. And we are not going to do that in this course, at least not in the beginning. Write a test code that runs the actual code with inputs for which we know the output. And then we can check if the program is correct. Okay, so this is a big field. It's called software testing. And yeah, so this is the only strategy that we can use to find semantic errors. But now I will give you an example. So going back to our very first example in this course where we wanted to find the average of all the even numbers in a list. Let's do that here. So let's maybe go back and first create a list of numbers. And let's create the same list of numbers from before. So let's say 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1 and 4. And now remember how we built up the program back then. So what we did is we wrote a code like this. We said 4 numbers in numbers. So we loop over the numbers in the list. Then we said if number modulo divided by 2 has no rest, then the number is even. And then for every even number what we do is we initialize two variables called total and count. And we set them both equal to 0 before the for loop. And then inside the for loop what we're going to do is we want to go ahead and calculate the sum of all the even numbers and the count of all the even numbers. So let's go ahead and say in every iteration of the loop when we encounter an even number we go ahead and we take the old count, add one to it and store the number as the new count. And then for our total we go ahead and take our old total and we add to that, for example, the count. And then after the for loop we will go ahead and we will say we divide the total of the even numbers by the count of the even numbers. And remember that the numbers here are the numbers from 1 to 12, all the numbers from 1 to 12 and if you calculate the average of all the even numbers here this is going to be 7.0 if you remember correctly. So let's run the code here and interestingly we get back 3.5. So there must be some error here. And the error is actually a little bit subtle but you could have probably guessed it and maybe you saw it as I made it. So of course I want to calculate the sum of all the even numbers and the count of all the even numbers and in order to calculate the sum of all the total numbers in every iteration of the loop if I encounter an even number I want to take the old total and of course I want to go ahead and add to it the current even number. So that is the error. And now I get back 7.0 on the real actual result. However as you saw I on purpose now of course made an error I chose the wrong variable. So instead of saying number here I accidentally said count. And we see that unfortunately the code runs. There is no error. But that's actually not correct when I say that because there is always an error. So whenever this code cell is being run no matter what the input is so that the numbers up here no matter what they are we will always get the wrong result because the code the entire program is just not correct. And as I said before the only way to figure this out is by executing the code with input for which we know the output. And for this list as we saw before we know that the solution should be 7.0. So here we see with the end result that something must be wrong. And just for your notes I will write here count is wrong should be number of course. Okay but I will leave it here in this file in a wrong way so that you will always remember that that is an example of a semantic error. And the term semantic means that something has a specific meaning to us humans and for a computer it really doesn't matter what it does. We tell the computer here that just add some numbers and it just adds them and it works. So we are not doing anything invalid here so that is why the code works perfectly but the program has an error in terms of the meaning to us as humans. That is why it's a semantic error. Okay so these are three different categories of errors. So syntax errors the nicest ones because we always see them runtime errors. These are the okay ones because we could handle them. So most of the time the code works but for some bad input data usually the program does not work and it crashes. So an example would be the divide by 0 and we get the red error message and for a semantic error under no circumstances do we get a red error message but the program the outcome of the program is of course always wrong. Okay maybe one last note regarding error messages. So when we are here in a Jupyter notebook when an error message occurs we get this red error message. What this really means is if we were to run the code in a different way as a real program so to say these red error messages would mean that in this second the program crashes. So you all have had this case where you wrote something in Word or you did some fancy calculation in Excel and you haven't saved your intermediate results and at some point X by for whatever reason Word or Excel just crashed or your browser crashes sometimes and you don't understand why did the program just crash and yeah this is usually because something goes wrong and usually in a real world program there should then be a runtime error because if it were a syntax error the programmers who developed the program and gave it to you they would already know because a syntax error means that the program would never ever run but a runtime error means that in some edge cases for bad input for a bad set of variables a program may crash and so whenever you see a red error message that if this were a program just like any other program you execute on your laptop in this second the program would just crash and die and all the intermediate state would get lost of course. So this is a short overview on the three big types of errors and I will see you in the next video.