Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

Python error IndentationError expected an indented block

Asked by Gordon Guess Jul 27, 2021 36.6K views 3 answers
Share

About this question

 I am trying to execute the following python code:

def example(p,q):
a = p.find(" ")
b = q.find(" ")
str_p = p[0:a]
str_q = p[b+1:]
if str_p == str_q:
    result = True
else:
    result = False
return result

And I get the following error:

IndentationError: expected an indented block

Why getting a python error expected an indented block?

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 7, 2025

The "IndentationError: expected an indented block" in Python occurs when the code structure is incorrect, usually due to missing or incorrect indentation. Python relies on indentation to define code blocks instead of braces {} like in other languages.

Common Causes & Fixes

1. Missing Indentation

  • Python requires an indented block after statements like if, for, while, def, etc.

❌ Incorrect:

if True:
print("Hello")  # No indentation

✅ Correct:

if True:
    print("Hello")  # Indented correctly

2. Inconsistent Indentation (Tabs vs Spaces)

  • Mixing spaces and tabs can cause errors.
  • Python recommends 4 spaces per indentation.
  • Use an editor like VS Code or PyCharm to automatically convert tabs to spaces.

3. Empty Code Blocks

  • If a block is left empty, Python expects at least one statement.
  • Use pass to avoid errors.

❌ Incorrect:

def my_function():
    # No code inside

✅ Correct:

def my_function():
    pass  # Placeholder to avoid errors

4. Incorrect Indentation Level

  • Ensure all statements in a block have the same indentation level.

❌ Incorrect:

for i in range(3):
    print(i)
      print("Indented incorrectly")

✅ Correct:

for i in range(3):
    print(i)
    print("Indented correctly")

Fix: Always use consistent indentation and check for missing blocks!


Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 25, 2024

The "IndentationError: expected an indented block" is a common error in Python that occurs when the interpreter encounters a line of code that should be indented within a block but finds that the line is not indented properly or not indented at all.


Here's an example of code that might trigger this error:if condition:
print("Condition is true")  # This line should be indented within the block

To resolve this error, you need to ensure that all lines within a block are properly indented. In Python, indentation is crucial for defining the structure of the code, such as loops, conditional statements, and function definitions. Here's the corrected version of the code:

if condition:
    print("Condition is true")  # Proper indentation

In this corrected version, the print statement is indented with four spaces (or a tab), indicating that it belongs to the block defined by the if statement.








Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.