Debugging
Below are some of the common errors that you will find when using Python with how to overcome them.
EOL while scanning string literal
Error:
Reason: this has happened as there is a missing speech marks after the string before the close bracket. IDLE will highlight the line that it is on for you.
Solution: Add the speech marks in the correct place. The change on this example is shown below:
Error:
Reason: this has happened as there is a missing speech marks in the print
message between the string and the variable name.
Solution: Add the speech marks in the correct place. The change on this example is shown below:
Invalid Syntax
Error:
Reason: this has happened as there is an incorrect number of close brackets at the end of the line of code. As there is two open brackets (one for the float function and one for the input function) there needs to be two at the end.
Solution: Add the bracket in the correct place. When IDLE highlights the error, it is normally on the line above where it highlights it. The change on this example is shown below:
Error:
Reason: this has happened as the string in the print
message has not been joined with the round
function, a +
or ,
is missing.
Solution: Add the +
or ,
in the correct place. When IDLE highlights the error, the error is normally just before. The change on this example is shown below:
Error:
Reason: this has happened as a bracket has been missed at the end of the print
command.
Solution: Add the )
in the correct place. When IDLE highlights the error, it is normally on the line above where it highlights it. The change on this example is shown below:
Error:
Reason: this has happened as you should have a double equals sign ==
. When comparing two values in Python you must use a double equals.
Solution: Add another =
in the correct place. When IDLE highlights the error, it is showing the exact location of the problem. The change on this example is shown below:
Error:
Reason: this has happened as after an if statement with the condition check it should have a colon :
.
Solution: Add a colon :
in the correct place. When IDLE highlights the error, it is directly in front of where the red line ends. The change on this example is shown below:
Error:
Reason: this has happened as a colon :
is needed after the end of a line with while
on it.
Solution: Add a colon :
in the correct place. When IDLE highlights the error, it is directly infront of where the red line ends. The change on this example is shown below:
Error:
Reason: this has happened as an else
should have no condition after it. If you want to have a condition it should be an elif
.
Solution: As it is the last check it should just be else:
. The change on this example is shown below:
NameError: name ‘variable’ is not defined
Error:
Reason: this has happened as the variable has been used later in the program but has not been defined at the start and given a starting value.
Solution: Define the variable at the start of the code and set it to a starting value, e.g. 0. The change on this example is shown below:
Error:
Reason: this has happened as the variable that you have used in line 2 doesn’t exist. IDLE lets you know what line the error is on. The if
refers to mark and the user input is stored as score
.
Solution: Change the name of the variable to the correct name that you want to refer to, in this case score
. The change on this example is shown below:
Error:
Reason: this has happened as the variable that you have used in line 6 doesn’t exist. IDLE lets you know what line the error is on. The variable defined at the start is attempts
and when it is used in the code is referred to as attempt
.
Solution: Change the name of the variable in line six to attempts
to match the name of the variable on line 3. The change on this example is shown below:
expected an indented block
Error:
Reason: this has happened as the code that is to be executed inside a loop must be indented.
Solution: Indent the code that you want to execute inside the loop. The change on this example is shown below:
Error:
Reason: this has happened as the code that is be executed after an if
must be indented.
Solution: Indent the code that you want to execute if the condition is met under the if
. The change on this example is shown below:
Error:
Reason: this has happened as the code has not been correctly indented. The code for the while
loop is indented correctly, however there needs further indentation for code under the if
.
Solution: Indent the code that you want to execute if the condition is met under the if
. The change on this example is shown below:
TypeErrors
Error:
Reason: this has happened as on line 4 it has tried to use height in a calculation, but height is stored as a string, the data type of height
is a string
.
Solution: convert the input to a float
or int
so it is considered a number and can be used in calculations.
Error:
Reason: this has happened as on line 5 as it has tried to use height in a print
command. It is joining a string with a float and therefore it gives an error. All variables in a print command must be converted to a str()
to prevent the error.
Solution: convert the variable height
to a string str(height)
in the print command.
IOError
Error:
Reason: this has happened as the file that is trying to be read cannot be found. This means that the .csv file is not in the same folder as the Python program.
Solution: make sure the .csv
file that you are trying to read is in the same folder as the Python program.