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")
print(data)
However, when executed, the code produces the following output:
File "script2.py", 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:
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
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.