The correct way to fix a python error

Did you know that even the most experienced programmers make errors in their programs?

However, one of the things that separates them from novices is that they know how to fix an error. Not knowing how to fix an error will not only waste a lot of time, but even worse, it can demotivate you to the point where you feel dumb and give up learning how to program. In this reading, you will learn the secrets of finding and fixing a Python code error.

I like examples, so let’s take a look at one. The Python code below is supposed to create a dictionary and print that dictionary out.

data = ("Name":"John", "Surname":"Smith")

However, when executed, the code produces the following output:

File "", line 1
data = ("Name":"John", "Surname":"Smith")
SyntaxError: invalid syntax

That’s the entire error message you got. Now here are the steps on how to understand and solve that error:

  1. Read the error from the beginning. The first line tells you the location of the error. So, the error happened in (that was the name of my script), on line 1. Now you know where the error occurred. For your convenience, you also have the line that caused the error printed out in the second line of the error message.
  2. Next, look at the error type. In this case, the error type is a SyntaxError. That means you have written something that doesn’t follow the Python syntax rules. So, now you have an idea of what error you are dealing with. For an overview of possible Python error types, you can look here.
  3. Look at the details of the error. On the right of SyntaxError you have the detailed information about the error. In this case, this information is "invalid syntax"and you also have an arrow character pointing upward. That error is pointing to the colon character. The arrow is trying to say that the colon doesn’t belong there.
  4. Time to use your logic. Now, the Python interpreter gave you all the information that a robot can give. Now it’s your turn as a human to use your logic to fix the error. So, Python executes a script from top to bottom, line by line, and reads each line from left to right. In this case, it started to read the first line, and it detected round brackets after the assignment operator. That means you are creating a tuple. That’s fine. But then after you write the first item (“Name” in this case). You were supposed to write a comma to separate that item from the next item, but you used a colon instead, so the interpreter is saying that a colon is not syntactically correct to use with round brackets.

Therefore, you should make up your mind to either write a tuple like:

 data = ("Name", "John", "Surname", "Smith")

Or a dictionary of key-value pairs, like:

 data = {"Name":"John", "Surname":"Smith".  

The decision is up to you. In this case, though, I believe the programmer meant to write a dictionary, so I am going to replace the round brackets with curly brackets because I know a dictionary is defined through curly brackets.

That’s the formula to fix an error. Sometimes though errors are much more complex than this, so in that case copying and pasting the last line of the error (SyntaxError: invalid syntax in this case ) on Google will usually show up forums posts with answers on fixing that particular error. However, that should be the last resort, as it’s better to use your knowledge first.

Next Lecture
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.