1.7. Syntax errorsΒΆ
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.
Check your understanding
- Attempting to divide by 0.
- 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.
intro-7-1: Which of the following is a syntax error?
- The programmer.
- 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.
intro-7-2: Who or what typically finds syntax errors?