IndentationError: expected an indented block in Python – How to Resolve it

Hi friends! In this post, we will be discussing about the “IndentationError: expected an indented block” in Python and how to easily resolve it, as well as, how to resolve the “IndentationError: unexpected indent” error.

 

What is Indentation in Python?

Generally, in programming, indentation is when you leave empty spaces in the beginning of a code line.

In many programming languages, indentation in code only contributes to a more readable code.

However, in Python, indentation is very important since it actually indicates a block of code.

 

Example of  Code Indentation in Python

Now let’s see some examples of how indentation affects a Python program’s behaviour.

Below, we will see some code examples in Python, that include an if…else statement.

 

Code Indentation Example 1 – Gives an Error

Normally, in the below code, we should have used indentation for the if…else code blocks, but we didn’t, in order to replicate the error.

So, let’s see the code:

# Indentation Example in Python

# Variable
x=10 

# if...else statement
if x > 5:
print ("\n x is greater than 5")
else:
print ("\n x is less than 5")

 

If we run the above code, we will get an error message similar to the below:

    print ("\n x is greater than 5")        
    ^
IndentationError: expected an indented block

 

The reason we get the above error message, is the lack of indentation in the code, that is the if…else code blocks are not correctly formed.

As you can see, there’s not indentation in the two “print” code lines.

 

Code Indentation Example 2: Works OK

Now let’s try to fix our Python code, based on the above observations, that is inserting a space in the two “print” lines.

# Indentation Example in Python

# Variable
x=10 

# if...else statement
if x > 5:
 print ("\n x is greater than 5")
else:
 print ("\n x is less than 5")

As you can see in the above code example, we have added an empty space in the two code blocks of the if…else statement (lines 8 and 10).

Therefore, now, if we execute the above code we get the output:

x is greater than 5

 

Where is Code Indentation Required in Python?

Code indentation is required, every time you need to write a code block in Python.

Some examples include:

  • In if…else statements
  • In exception handling code constructs
  • When defining methods
  • In “While” loops

 

Important Consideration about Code Indentation in Python

You need to use the same number of spaces in the same block of code.

In a different case,  Python will give another indentation-related error, that is: IndentationError: unexpected indent

Let’s see a relevant example.

Python code example with correct indentation – Runs OK:

# Indentation Example in Python

# Variable
x=10 

# if...else statement
if x > 5:
 print ("\n x is greater than 5")
 print ("\n the value of x is: ",x)
else:
 print ("\n x is less than 5")
 print ("\n the value of x is: ",x)

 

Python code example with wrong indentation – Gives an Error:

# Indentation Example in Python

# Variable
x=10 

# if...else statement
if x > 5:
 print ("\n x is greater than 5")
  print ("\n the value of x is: ",x)
else:
 print ("\n x is less than 5")
  print ("\n the value of x is: ",x)

 

Learn more about SQL Server Data Access from Python – Enroll to our Course!

Enroll to our online course  “Working with Python on Windows and SQL Server Databases” with an exclusive discount, and get started with Python data access programming for SQL Server databases, fast and easy!

Working with Python on Windows and SQL Server Databases - Online Course
(Lifetime Access, Q&A, Certificate of Completion, downloadable resources and more!)

Enroll with discount!

 

 

Featured Online Courses:

 

Read Also:

 

Check our online courses!

Check our eBooks!

Subscribe to our YouTube channel!

Subscribe to our newsletter and stay up to date!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub