Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 0.7 — Reading Error Messages and Tracebacks
Python · Example 2 of 3

A syntax error: nothing runs at all

Step 1 is not printed, although its line is correct. Python checks the whole file's grammar first. Add the missing quote.

When Python cannot run a line it stops and prints an error message. Read it from the bottom: the last line names the kind of error and what went wrong, and the lines above it (the traceback) show the file and line number where it happened.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

This program should print Total: 50 but crashes. Read the error, then fix it: line 1 price = 25, line 2 total = price * 2, line 3 print("Total: " + total).

02

Without running it, say what this prints, including the error type: line 1 print("A"), line 2 print(10 / 0), line 3 print("B").

03

A program fails with NameError: name 'totl' is not defined. Did you mean: 'total'? on line 7. What is the most likely mistake, and how do you fix it?

Explain it without notes

01

What is the difference between a syntax error and an exception?

02

In what order should you read a traceback, and what does each part tell you?

03

What does "most recent call last" mean?

04

Give the likely cause of each: NameError, TypeError, IndexError.

05

Why might the mistake not be on the line that the error reports?

A syntax error: nothing runs at all
Sign in to run this example in your browser.

Expected output

  File "main.py", line 2
    message = "Hello
              ^
SyntaxError: unterminated string literal (detected at line 2)