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?

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Sam
  • 934
  • 3
  • 10
  • 24
  • 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
  • 767,688
  • 176
  • 1,542
  • 1,434
  • 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.) – zigg 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 with no 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.

Pramod
  • 5,000
  • 3
  • 41
  • 43
  • 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.

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 docs 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.

Community
  • 1
  • 1
Kim Stacks
  • 9,785
  • 31
  • 141
  • 266
3

I got the same error, 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

Working fine. After TAB space.

MAX
  • 1,444
  • 4
  • 15
  • 22
0

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

wuliang
  • 711
  • 5
  • 7
  • I user Pyscripter in windows. Because I have to do other jobs in windows although I'm really fond of using linux. – Sam Apr 20 '12 at 01:33