Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "IndentationError: expected an indented block"? [duplicate]

Tags:

python

if len(trashed_files) == 0 :     print "No files trashed from current dir ('%s')" % os.path.realpath(os.curdir) else :     index=raw_input("What file to restore [0..%d]: " % (len(trashed_files)-1))     if index == "*" :         for tfile in trashed_files :             try:                 tfile.restore()             except IOError, e:                 import sys                 print >> sys.stderr, str(e)                 sys.exit(1)     elif index == "" :         print "Exiting"     else :         index = int(index)         try:             trashed_files[index].restore()         except IOError, e:             import sys             print >> sys.stderr, str(e)             sys.exit(1) 

I am getting:

        elif index == "" :         ^     IndentationError: expected an indented block 
like image 601
ParoX Avatar asked Dec 15 '10 03:12

ParoX


People also ask

How do I fix IndentationError expected an indented block?

If you are using Sublime, you can select all, click on the lower right beside 'Python' and make sure you check 'Indent using spaces' and choose your Tab Width to be consistent, then Convert Indentation to Spaces to convert all tabs to spaces.

What does expected an indented block mean?

expected an indented block This means that a function must have at least one line of code. It also means that a conditional must have at least one line of code to run if the condition is true.

How do you avoid unexpected indent errors in Python?

The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.


2 Answers

As the error message indicates, you have an indentation error. It is probably caused by a mix of tabs and spaces.

like image 99
mpen Avatar answered Sep 23 '22 12:09

mpen


There are in fact multiples things you need to know about indentation in Python:

Python really cares about indention.

In a lot of other languages the indention is not necessary but improves readability. In Python indentation replaces the keyword begin / end or { } and is therefore necessary.

This is verified before the execution of the code, therefore even if the code with the indentation error is never reached, it won't work.

There are different indention errors and you reading them helps a lot:

1. "IndentationError: expected an indented block"

They are two main reasons why you could have such an error:

- You have a ":" without an indented block behind.

Here are two examples:

Example 1, no indented block:

Input:

if 3 != 4:     print("usual") else: 

Output:

  File "<stdin>", line 4      ^ IndentationError: expected an indented block 

The output states that you need to have an indented block on line 4, after the else: statement

Example 2, unindented block:

Input:

if 3 != 4: print("usual") 

Output

  File "<stdin>", line 2     print("usual")         ^ IndentationError: expected an indented block 

The output states that you need to have an indented block line 2, after the if 3 != 4: statement

- You are using Python2.x and have a mix of tabs and spaces:

Input

def foo():     if 1:         print 1 

Please note that before if, there is a tab, and before print there is 8 spaces.

Output:

  File "<stdin>", line 3     print 1       ^ IndentationError: expected an indented block 

It's quite hard to understand what is happening here, it seems that there is an indent block... But as I said, I've used tabs and spaces, and you should never do that.

  • You can get some info here.
  • Remove all tabs and replaces them by four spaces.
  • And configure your editor to do that automatically.

2. "IndentationError: unexpected indent"

It is important to indent blocks, but only blocks that should be indent. So basically this error says:

- You have an indented block without a ":" before it.

Example:

Input:

a = 3   a += 3 

Output:

  File "<stdin>", line 2     a += 3     ^ IndentationError: unexpected indent 

The output states that he wasn't expecting an indent block line 2, then you should remove it.

3. "TabError: inconsistent use of tabs and spaces in indentation" (python3.x only)

  • You can get some info here.
  • But basically it's, you are using tabs and spaces in your code.
  • You don't want that.
  • Remove all tabs and replaces them by four spaces.
  • And configure your editor to do that automatically.


Eventually, to come back on your problem:

Just look at the line number of the error, and fix it using the previous information.

like image 28
Xavier C. Avatar answered Sep 20 '22 12:09

Xavier C.