Python: Syntax errors
If a Python program is written with a syntax violation, the interpreter stops and displays an error message. The message tells you the type of error, the file and line where it occurred, and often marks the exact spot where the interpreter got stuck.
What is a syntax error?
A syntax error (SyntaxError) happens when code breaks the grammatical rules of the language: an unclosed string, a missing parenthesis, characters in the wrong order, and so on.
Code with error Interpreter Result
┌──────────────┐ ┌─────────────┐ ┌──────────────────┐
│ print('Hi' │ ──→ │ Python │ ──→ │ SyntaxError: │
└──────────────┘ └─────────────┘ │ unexpected EOF │
└──────────────────┘In natural languages, text with errors can usually be understood from context. In programming, the rules are strict: even a tiny violation makes the code impossible to run. Here is a simple example:
print('Hodor)The closing quote is missing. Running this produces:
$ python index.py
File "index.py", line 1
print('Hodor)
^
SyntaxError: EOL while scanning string literalThe error message may look unfamiliar at first, but that's fine — the more you encounter these messages, the faster you'll understand them at a glance.
Why syntax errors are considered easy
Syntax errors are tied to the rules of writing code rather than to its meaning, so they're straightforward to fix: find the violation and correct it. Many editors highlight syntax errors in real time, making them even easier to spot.
There is one nuance though: the interpreter doesn't always point to the exact location of the error. Sometimes the problem is a few lines above where the message appears. For example, an unclosed parenthesis on one line can "break" everything that follows.
What to do when you see a syntax error
- Read the error message — it almost always contains useful information.
- Check the line the message points to.
- Check the line above it — the error is sometimes hidden there.
- Use an editor with syntax highlighting, like VS Code — it helps catch unclosed quotes and brackets immediately.
Instructions
This assignment is not directly related to the lesson. But it will be useful to practice with the output on the screen.
Display:
What Is Dead May Never DieIf you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Your exercise will be checked with these tests:
import runpy
def test(capsys):
expected = "Программа успешно запущена"
runpy.run_module('solution')
out, _ = capsys.readouterr()
assert out.strip() == expectedTeacher's solution will be available in:
20:00
