19

Here is the code:

def myfirst_yoursecond(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

Here is the error:

Traceback (most recent call last):
  File "vm_main.py", line 26, in <module>
    import main
  File "/tmp/vmuser_ssgopfskde/main.py", line 22
    result = False
         ^
IndentationError: expected an indented block

What's wrong with my code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam
  • 964
  • 3
  • 11
  • 24
  • 1
    Does this answer your question? [Why am I getting "IndentationError: expected an indented block"?](https://stackoverflow.com/questions/4446366/why-am-i-getting-indentationerror-expected-an-indented-block) – Tomerikoo Dec 29 '20 at 12:36

5 Answers5

37

You've mixed tabs and spaces. This can lead to some confusing errors.

I'd suggest using only tabs or only spaces for indentation.

Using only spaces is generally the easier choice. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on.


As an aside, your code is more verbose than it needs to be. Instead of this:

if str_p == str_q:
    result = True
else:
    result = False
return result

Just do this:

return str_p == str_q

You also appear to have a bug on this line:

str_q = p[b+1:]

I'll leave you to figure out what the error is.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Thank you!! It works! Python doesn't support both indentations. It sounds strange. – Sam Apr 20 '12 at 00:49
  • It does actually support 8-space tabs, but it's much better to stick to just spaces, particularly if your code moves between editors and systems (which you should expect it to.) – Mattie Apr 20 '12 at 00:56
  • I'm sorry. I have changed it to q[b+1:]. It's really kind of you to help me a lot. – Sam Apr 20 '12 at 01:27
  • I get the same problem about no indent before a def bla(x,y) function line below. Just adding indentation does work. Thank you. – m3nda May 21 '15 at 01:55
13

This error also occurs if you have a block without any statements in it.

For example:

def my_function():
    for i in range(1,10):


def say_hello():
    return "hello"

Notice that the for block is empty. You can use the pass statement if you want to test the remaining code in the module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pramod
  • 5,150
  • 3
  • 45
  • 46
  • 1
    I was looking for a statement similar to bash's ':' statement, not easy to formulate a search term for that; came here immediately, and your answer stands out for mentioning a pass stament that I didn't know. Hence up one. Thank you. –  Jun 29 '15 at 20:45
3

If you are using a Mac and Sublime Text 3, this is what you do.

Go to your /Packages/User/ and create a file called Python.sublime-settings file.

Typically /Packages/User is inside your ~/Library/Application Support/Sublime Text 3/Packages/User/Python.sublime-settings if you are using Mac OS X.

Then you put this in the Python.sublime-settings.

{
    "tab_size": 4,
    "translate_tabs_to_spaces": false
}

Credit goes to Mark Byer's answer, Sublime Text 3 documentation and Python style guide.

This answer is mostly for readers who had the same issue and stumble upon this and are using Sublime Text 3 on Mac OS X.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
2

I got the same error, and this is what I did to solve the issue.

Before Indentation:

Enter image description here

Indentation Error: expected an indented block.

After indentation:

Enter image description here

It is working fine. After TAB space.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MAX
  • 1,562
  • 4
  • 17
  • 25
  • 1
    Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/38032036/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Jun 08 '23 at 09:17
0

You should install a editor (or IDE) supporting Python syntax. It can highlight source code and make basic format checking. For example: Eric 4, Spyder, Ninjia, or Emacs, Vi.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wuliang
  • 749
  • 5
  • 7