3 minutes read

At this point, you probably already know that writing code sometimes might be frustrating because not everything works as you'd expect. So, in this topic, we'll finally examine the main reason for this frustration — errors.

The first thing you need to know about programming is how to print Hello, world!. The second one is that this might be challenging, as even such a tiny script can contain various errors. Here you are:

print("Hello, world!"

If you run this code, you'll get this:

Traceback (most recent call last):
  File "FULL/PATH/TO_YOUR/SCRIPT.PY", line 1
    print("Hello, world!"
                        ^
SyntaxError: unexpected EOF while parsing

Traceback is a stack trace that appears when your code causes an error. It reports detailed information on that particular error, indicating the specific files in which the error occurred. Nonetheless, the most informative lines for us right now are the last two. They point out the mistake in your code.

This might seem a bit frustrating, but errors are generally designed to allow Python to communicate with you. Whenever you see those threatening red lines – don't panic! Just follow what they are saying.

Syntax errors

In the example above, we can see the magic word SyntaxError that is likely to haunt you throughout getting used to Python. A large variety of different errors are referred to as syntax errors. They usually mean that Python has encountered a problem while trying to compile your program and can't even execute it.

If you carefully read the text of a traceback, it will help you find mistakes and correct them quite quickly, as you can see an arrow pointing to the exact place where Python found the mistake in your code. Every syntax error has an associated value. It describes an error in detail. In the example, EOF in the message SyntaxError: unexpected EOF while parsing stands for the end of a file. This message means that something else had been expected after your statement, but you didn't pass it to the interpreter. In our case, there should've been a closing round bracket ).

Mistakes won't be so obvious all the time. The message you'll get as an associated value will likely be the most common and obscure invalid syntax which isn't really helpful. To locate the issue, it's enough to know that the error is in the syntax.

Common errors for beginners

Some of the most common syntax errors are:

  • Wrong spelling of keywords and function names, e.g., While instead of while, pint instead of print;
  • the wrong number of parentheses in function calls, e.g., print just one round bracket;
  • Indents are also fertile soil for errors. Therefore, use spaces and tabs carefully;
  • Quotations marks. Don't forget to wrap a string in quotation marks of the same type: triple quotes for multi-line strings or double or single quotes for ordinary strings.

Modern IDEs check everything for you and kindly highlight where you have made a mistake or typo, but don't rely on this too much, and be ready to read the traceback yourself.

Mind that Python stops compiling your program after finding the first Syntax error, so it might take a while to fix every single mistake.

Check the following piece of code, for example. It looks like a Petri dish of syntax errors:

missing_quote  = "this is a mistake!
another_string = this is not a string!"
parted_string = 'I'd like to be highlighted, but'
          prnit("I am not")

Take a deep breath, reread the article, and continue perfecting your programming skills! As you can see, there are plenty of syntax errors in this tiny piece of code. If you've checked and corrected everything from the list above and yet you encounter those error messages – don't worry! Once again, it's just Python trying to tell you that something went wrong.

Conclusion

In this topic, we learned what traceback is, looked at syntax errors in detail, and went through several common mistakes you, as a beginner, may encounter.

1017 learners liked this piece of theory. 8 didn't like it. What about you?
Report a typo