Why does VS Code sometimes show an 'IndentationError: expected an indented block' message?

Responsive Ad Header

Question

Grade: Education Subject: Support
Why does VS Code sometimes show an 'IndentationError: expected an indented block' message?
Asked by:
90 Viewed 90 Answers

Answer (90)

Best Answer
(513)
This error message in VS Code typically arises when Python expects a block of code to follow a statement that introduces a new scope, such as `if`, `for`, `while`, `def`, or `class`, but it encounters either no indentation or incorrect indentation on the next line. For instance, writing `if True:` and then immediately starting a new, unindented statement on the line after would cause this. To fix it, ensure the code immediately following these statements is indented by a consistent amount (usually 4 spaces).