Chapter 2 Lab Exercise
13.1 Dealing with Syntax Errors
It is very common to run into different types of errors while writing programs. The Python interpreter does its best to let you know the type of the error and where it is located.
In this final chapter, we will discuss the most common type of errors:
syntax errors.
Syntax errors are errors that emerge due to language and expression syntax that was written incorrectly by the programmer. To see an example of syntax errors, run the following code inside Eclipse:
13.1 Dealing with Syntax Errors
First we assign the string ‘John’ to the variable name and then we make a test using the if statement to see if the variable’s value is equal to
“John” and print out some text if the test succeeds. Then, we create two variables, i and j, assign values to them, do some math operations and print out the returned output.
However, running this code returns an error:
Figure 13.1: A syntax error detected at the second line around the assignment operator.
Read the issued error carefully. In the first line of the error (displayed in blue), the interpreter lets you know that there is an error in the second line of your code. This corresponds to the line if name = ‘John’. Moreover, you notice an arrow marker (^) which is pointing towards the assignment sign (=). This arrow means that you should review your code at that part.
The job of the interpreter ends here, and now it is your turn to look at the part of the code as guided by the interpreter.
If you remember from previous chapters, the assignment operator (=) is actually used to assign a value to a variable—it is not an equal sign.
If you want to test whether something is equal to something else, you should use the equal sign (==) which is a like a double assignment operator. Keeping this in mind, we correct our code as follows:
name = 'John' if name == 'John'
print('Your name is John') i = 5
j = 15
k = ((i*j) + (j+i) print(k)
invalid syntax
191 191 Even though we corrected the error that was pointed out, we still get another error when we run this code:
Figure 13.2: A syntax error is detected at the end of the second line.
Notice that we still have an error at the second line, but this time at the end of it. The interpreter does its best and gives us the approximate location of the error. Now, you need to take another cautious look at the code, and you should be able to realize that a colon (:) meant to be at the end of an if statement is missing. After you add the colon, the code should look like this:
name = 'John'
if name == 'John':
print('Your name is John') i = 5
j = 15
k = ((i*j) + (j+i) print(k)
Running the code again, you will come across another syntax problem:
Figure 13.3: A syntax error is detected at the ninth line around the print function.
13.1 Dealing with Syntax Errors
This time, the error occurred at the ninth line. Here is where you have to be more cautious because this time the interpreter is giving a rough estimation—the error is not exactly on the line where the arrow marker is pointing. This time, you should look at the expression that comes before the print command. This corresponds to this line:
k = ((i*j) + (j+i)
Notice that a bracket is missing at the end of the line. Go ahead and add it:
name = 'John'
if name == 'John':
print('Your name is John') i = 5
j = 15
k = ((i*j) + (j+i)) print(k)
Once you have added the missing bracket and ran the code, you should get this result:
Figure 13.4: An indentation error is detected at the last line before the print command.
This time the Python interpreter detected an indentation error. An indentation error is another kind of syntax error that happens due to the incorrect usage of blank space. In this case, the print command is not part of any loop, conditional, class or function. Therefore, it should not be indented.
Correct this error by finding and deleting the improper blank space,
unexpected
indents
193 193 and finally you have managed to correct the whole block which looks as follows.
name = 'John'
if name == 'John':
print('Your name is John') i = 5
j = 15
k = ((i*j) + (j+i)) print(k)
Finally, we get an output free of errors:
Figure 13.5: Generated output after correcting all syntax errors.
At this point, you have made sure that the code doesn’t have any syntax errors.
Tip: An output that is generated without errors is an indication that there are no syntax error in the code. However, there may be a different kind of errors that cannot be detected by computers, but instead need to be noticed by a human being.
Questions for Review
1. What is the order that the interpreter checks for syntax errors?
a. Checking classes, functions, and then variables.
b. Checking from bottom lines to top.
c. Checking from top lines to bottom.
d. There is no specific order.
Questions for Review
13.1 Dealing with Syntax Errors
2. What is true about blank space in Python?
a. It is always crucial.
b. It is always ignored.
c. It is ignored only if no more than four blank spaces are used.
d. It is only crucial for indentation purposes.
Lab Activity
The following code is supposed to print all the elements of the string
“John” one by one:
for i inside "John":
print i
However, the code contains two syntax errors. Try to debug them and provide the correct code.
l
ab soluTionfor i in "John":
print(i)
And here is the output:
Figure 13.6: String elements printed out after the code has been debugged.
Lab Activity
195 195