Python builtins indentationerror expected an indented block

Python error expected an indented block | How to avoid it

At Bobcares, we offer solutions for every query, big and small, as a part of our Server Management Service.

Let’s take a look at how our Support Team recently helped a customer with the commonly seen indentation error in Python error.

What is Python error: “Expected an indented block”?

If you have been using spaces and tabs incorrectly, you are sure to run into the “IndentationError: expected an indented block” error. Today, our Support Engineers are here to walk you through the error and help you figure out how to avoid it.

Читайте также:  Define checked exception java

While other programming languages like JavaScript and C rely on curly braces to structure code blocks, Python relies on indentation. Furthermore, the indentation makes it easier to read Python code.

This is why it is critical to master Python indentation rules in order to stop running into the “Expected an indented block”.

How to resolve Python error: “Expected an indented block”

The main purpose of the IndentationError is to let us know when we incorrectly indent our code. As long as we indent the code correctly as seen in the image below, we can avoid the error:

Python error expected an indented block

For instance,

site if site == 'edu': print('Logging in to Springfield School!') else: print('Please type the URL again.') print('You are ready to go!')

This code will result in the “IndentationError: expected an indented block”. By adding the following indentation, we will be able to avoid the error:

site if site == 'edu': print('Logging in to Springfield School!') else: print('Please type the URL again.') print('You are ready to go!')

Hence, we need to indent the code correctly in order to stay clear of the indentation error. According to our Support Team, ensuring that our code has the proper number of indents is all we need to do to make sure we do not face the error again.

[Looking for a solution to another query? We are just a click away.]

Conclusion

In essence, the skilled Support Engineers at Bobcares demonstrated how to deal with the indentation error in Python.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

Источник

[Solved] IndentationError: Expected An Indented Block Error

expected an indented block

Error handling is one of the best features of Python. With known error Exceptions, you can reduce the bugs in your program. As Python operates on indentation blocks for deducing the inside block for any statement, you may encounter IndentationError: Expected An Indented Block Error.

IndentationError: Expected An Indented Block Error is a known error in python which is thrown when an indented block is missing from the statement. IndentationError states that there is an error related to the Indentation of general statements in your code. In Python, general statement blocks expect an indentation in child statements. If you fail to provide these indentations, Indentation Error will arise.

In this tutorial, we will be discussing a new type of error, i.e., IndentationError: expected an indented block. We all know c, c++, and java when we write any loop, conditional statements, or function code inside the brackets. But in python, it is actually part of this programming language.

What is meant by Indentation?

The meaning of Indentation in python is the space from margin to the beginning of characters in a line. Where in other programming languages, indentation is just for the sake of the readability purpose. But in python, indentation is necessary.

What is IndentationError: Expected an indented block?

In most popular programming languages like c, c++, and java, spaces or indentation are just used to make the code look good and be easier to read. But In Python, it is actually part of this programming language. Because python is the sensitive language for indentation, many beginners face confusion or problems in the starting as Putting in extra space or leaving one out where it is needed will surely generate an error message. Some causes of indentation error are:

  • When we forget to indent the statements within a compound statement
  • When we forget to indent the statements of a user-defined function.

The error message IndentationError: expected an indented block would seem to indicate that you have a spaces error or indentation error.

Examples of IndentationError: Expected an indented block

Here are some examples through which you will know about the Indentation error: expected an indented block.

1. IndentationError: Expected an indented block in IF condition statements

In this example, we will be using the if condition for writing the code and seeing the particular error. We have taken two variables, ‘a’ and ‘b,’ with some integer value. Then, applied if condition and no indented the if block. Let us look at the example for understanding the concept in detail.

a=10 b=20 if b>a: print("b is greater than a")

IndentationError: Expected an indented block in IF condition statements

Explanation:

  • Firstly, we have taken two variables, ‘a’ and ‘b,’ and assigned the values 10 and 20 in them.
  • Then, we have applied if condition.
  • And at last, without giving the indentation block of if statement we have printed b is greater than a.
  • Hence, we have seen the output as IndentationError: expected an indented block.

2. If-else condition for seeing the error as expected an indented block

In this example, we will be using the if-else condition for writing the code and seeing the particular error. We have taken two variables, ‘a’ and ‘b,’ with some integer value. Then, applied the if-else condition and indented the if block but not the else block. So let’s see which error occurs. Let us look at the example for understanding the concept in detail.

If-else condition for seeing the error as expected an indented block

a=10 b=20 if b>a: print("b is greater than a") else: print("b is smaller than a")

Explanation:

  • Firstly, we have taken two variables, ‘a’ and ‘b,’ and assigned the values 10 and 20 in them.
  • Then, we have applied the if-else condition.
  • In the if condition, we have applied the tab space but not in the else condition.
  • And at last, without giving the indentation block of else statement, we have tried to print b is smaller than a.
  • Hence, we have seen the output as IndentationError: expected an indented block.

3. Indentation Error: expected an indented block in Docstring Indentation

In this example, we will be showing that the error can also come up if the programmer forgets to indent a docstring. Docstrings must be in the same line with the rest of the code in a function. The Docstring processing tools will strip an amount of indentation from the second and further lines of the docstring, equal to the minimum indentation of all unblank lines after the first line. Let us look at the example for understanding the concept in detail.

def pythonpool(): """This is a comment docstring""" print("Hello") #fixing this error as #def pythonpool(): # """This is a comment docstring""" # print("Hello")

4. Indentation Error: expected an indented block in Tabbed Indentation

In this example, we will see the indentation error in the tabbed indentation. As you can see in the code, while writing “This is a comment docstring,” we have passed the tab space for an indent, and in print () there is less indent. so this will produce a tabbed indentation. Let us look at the example for understanding the concept in detail.

def pythonpool(): """This is a comment docstring""" print("Hello")

5. Indentation Error: expected an indented block in empty class/function

 Indentation Error: expected an indented block in empty class/function

In this example, we will see the error in an empty class. We have declared two classes with the name as EmptyClass and MainClass. But in Class EmptyClass, we didn’t pass (statement) any indent, so it generated the error. Let us look at the example for understanding the concept in detail.

#error code class EmptyClass: class MainClass: pass #solution code class EmptyClass: pass class MainClass: pass

Where Indentation is required?

The indentation is required in the python block. Whenever you encounter a colon(:) is a line break, and you need to indent your block. Python uses white space to distinguish code blocks. You are allowed to use spaces and tabs to create a python block. When several statements in python use the same indentation, they are considered as a block. Basically, Indentation is required to create a python block or write the loop, conditional statements, and user-defined function you require indentation.

How to solve the IndentationError: expected an indented block

To solve this error, here are some key points which you should remember while writing the program or code:

  • In your code, give the indent with only the tab spaces, equal to every loop, conditional, and user-defined function.
  • Otherwise, in your code, give the indent with only the spaces equal to every loop, conditional, and user-defined function.

Examples of solved IndentationError: expected an indented block

Here are some examples through which you will know how to solve the error Indentation error: expected an indented block.

1. If-else conditional statement indentation

In this example, we will be using the if-else condition for writing the code and seeing the particular output. We have taken two variables, ‘a’ and ‘b,’ with some integer value. After this, I applied the if-else condition with a proper indentation in both conditions and printed the output. Let us look at the example for understanding the concept in detail.

a=10 b=20 if b>a: print("b is greater than a") else: print("b is smaller than a")

Explanation:

  • Firstly, we have taken two variables, ‘a’ and ‘b,’ and assigned the values 10 and 20 in them.
  • Then, we have applied the if-else condition.
  • While applying the if-else condition, we have given the proper indentation of a tab in both the condition of if and as well as of else condition.
  • And printed the output after checking the condition.
  • Hence, we have seen the output without any IndentationError: expected an indented block error.

2. For loop statement indentation

In this example, we have applied for loop and printed the output while giving the proper indentation for the loop block. Let us look at the example for understanding the concept in detail.

Explanation:

  • In this example, we have applied for loop.
  • The for loop is starting from 1 and going till 10.
  • And with proper indent, we have printed the value of i.
  • Hence, you can see the correct output without any error.

How to fix indentation in some code editors

1. Sublime text

For setting the indentation in sublime text editor you need to perform the following steps:

To set the Indentaion to tabs

And go to the sub-menu and look for the ‘Indent Using Spaces’ option and uncheck it.

2. VS code

For setting the indentation in VS code text editor you need to perform the following steps:

  • Go to the File menu.
  • Choose preferences.
  • Choose settings.
  • Type tabsize in the search bar.
  • Then, uncheck the checkbox of Detect Indentation.
  • Change the tab size to 2 or 4 according to your choice.

3. Pycharm

For setting the indentation in Pycharm text editor you need to perform the following steps:

  • Go to the File menu.
  • Choose settings.
  • Then, Choose the editor.
  • Choose code style.
  • Choose Python.
  • And choose tabs and indents.
  • Suppose it is set to 0. please set it to 4. which is recommended by PEP8.

Conclusion

In this tutorial, we have learned the concept of IndentationError: expected an indented block. We have seen what Indentation is, what indentation error is, how indentation error is solved, why it is required to solve the indentation error. We have also explained the examples of showing the IndentationError: expected an indented block and the examples of showing the solution of the given error. All the examples are explained in detail with the help of examples.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Other Typical Python Errors

FAQs

1. What does expected an indented block mean in Python?

Excepted an indented block error in python, we must have at least one line of code while writing the function, conditional statements, and loops. We can also say that a conditional must have at least one line of code to run if the condition is true.

2. How to follow pep8 format to avoid getting IndentationError?

PEP8 formats says to you should follow 4 spaces indentation to avoid getting error.

3. Explain why this error is mostly generated in code editors like sublime.

This error is mostly generated in sublime as it does not follows PEP8 format, i.e., 4 spaces indentation. Sublime text editor follows tabs as indentation, so there are most chances to get the indentation error.

Источник

Оцените статью