Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors. Syntax errors are mistakes that violate the rules of the Python programming language. Runtime errors occur when your code is valid, but tries to do something that is impossible at the time the code is run (like divide by zero, or multiply two string variables together). Semantic errors occur when the program runs successfully but doesnβt produce the correct output, because the program that was written does not correctly solve the problem as intended. The following sections will describe these three types of errors in more detail.
Python can only execute a program if the program is syntactically correct; otherwise, the process fails and returns an error message. Syntax refers to the structure of a program and the rules about that structure. For example, in English, a sentence must begin with a capital letter and end with a period. this sentence contains a syntax error. So does this one
For most readers, a few syntax errors are not a significant problem, which is why we can read the poetry of e. e. cummings without problems. Python is not so forgiving. If there is a single syntax error anywhere in your program, Python will display an error message and quit. You will not be able to complete the execution of your program. During the first few weeks of your programming career, you will probably spend a lot of time tracking down syntax errors. However, as you gain experience, you will make fewer errors and you will also be able to find your errors faster.
A syntax error is an error in the structure of the python code that can be detected before the program is executed. Python cannot usually tell if you are trying to divide by 0 until it is executing your program (e.g., you might be asking the user for a value and then dividing by that value - you cannot know what value the user will enter before you run the program).
Forgetting a colon at the end of a statement where one is required.
This is a problem with the formal structure of the program. Python knows where colons are required and can detect when one is missing simply by looking at the code without running it.
Forgetting to divide by 100 when printing a percentage amount.
This will produce the wrong answer, but Python will not consider it an error at all. The programmer is the one who understands that the answer produced is wrong.
Programmers rarely find all the syntax errors, there is a computer program that will do it for us.
The compiler / interpreter.
The compiler and / or interpreter is a computer program that determines if your program is written in a way that can be translated into machine language for execution.
The computer.
Well, sort of. But it is a special thing in the computer that does it. The stand alone computer without this additional piece can not do it.
The teacher / instructor.
Your teacher and instructor may be able to find most of your syntax errors, but only because they have experience looking at code and possibly writing code. With experience syntax errors are easier to find. But we also have an automated way of finding these types of errors.
The second type of error is a runtime error, so called because the error does not appear until you run the program. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.
Python cannot reliably tell if you are trying to divide by 0 until it is executing your program (e.g., you might be asking the user for a value and then dividing by that value - you cannot know what value the user will enter before you run the program).
Forgetting a colon at the end of a statement where one is required.
This is a problem with the formal structure of the program. Python knows where colons are required and can detect when one is missing simply by looking at the code without running it.
Forgetting to divide by 100 when printing a percentage amount.
This will produce the wrong answer, but Python will not consider it an error at all. The programmer is the one who understands that the answer produced is wrong.
Programmers rarely find all the runtime errors, there is a computer program that will do it for us.
The interpreter.
If an instruction is illegal to perform at that point in the execution, the interpreter will stop with a message describing the exception.
The computer.
Well, sort of. But it is a special thing in the computer that does it. The stand alone computer without this additional piece can not do it.
The teacher / instructor.
Your teacher and instructor may be able to find most of your runtime errors, but only because they have experience looking at code and possibly writing code. With experience runtime errors are easier to find. But we also have an automated way of finding these types of errors.
The third type of error is the semantic error. If there is a semantic error in your program, it will run successfully in the sense that the computer will not generate any error messages. However, your program will not do the right thing. It will do something else. Specifically, it will do what you told it to do.
The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing.
A semantic error is an error in logic. In this case the program does not produce the correct output because the problem is not solved correctly. This would be considered a run-time error.
Forgetting a colon at the end of a statement where one is required.
A semantic error is an error in logic. In this case the program does not produce the correct output because the code can not be processed by the compiler or interpreter. This would be considered a syntax error.
Forgetting to divide by 100 when printing a percentage amount.
This will produce the wrong answer because the programmer implemented the solution incorrectly. This is a semantic error.
You must fully understand the problem so the you can tell if your program properly solves it.
The compiler / interpreter.
The compiler and / or interpreter will only do what you instruct it to do. It does not understand what the problem is that you want to solve.
The computer.
The computer does not understand your problem. It just executes the instructions that it is given.
The teacher / instructor.
Your teacher and instructor may be able to find most of your semantic errors, but only because they have experience solving problems. However it is your responsibility to understand the problem so you can develop a correct solution.