Skip to main content

Section 1.13 Semantic 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.
We don’t know enough Python yet to give good examples of semantic errors, so let’s give examples of commands you might give someone else that have the equivalent of “semantic errors”—instructions that the person can carry out, but that don’t get the task done correctly.
Example 1: You give someone these instructions to find the area of a rectangle:
  1. Measure the width of the rectangle and write it down.
  2. Measure the height of the rectangle and write it down.
  3. Add the two numbers you wrote down. That is the area.
The instructions are grammatically correct, and the person can do all of those actions, but they won’t get the right answer because you told them to add the numbers instead of multiplying them.
Example 2: If you are telling someone how to make a cake, and you give them these instructions:
  1. Put one-half cup of butter in a bowl.
  2. Put three cups of flour in the bowl.
  3. Mix the contents of the bowl.
  4. Pour the mixture into the cake pan.
  5. Put one-fourth cup of sugar into the bowl.
  6. Put the pan into a pre-heated oven at 350 degrees F for 25 minutes.
Again, the instructions are grammatically correct, and the person can do all of those actions, but they won’t get a good cake because the step with the sugar is in the wrong place.
The problem with these examples 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.
Unlike syntax and runtime errors, Python can’t help you find semantic errors because it has no way of knowing what you intended.

Checkpoint 1.13.1.

    Which of the following is a semantic error?
  • Attempting to divide by 0.
  • 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.

Checkpoint 1.13.2.

    Who or what typically finds semantic errors?
  • The programmer.
  • 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.