Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndentationError expected an indented block

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?

like image 782
Sam Avatar asked Apr 20 '12 00:04

Sam


People also ask

How do you fix indent expected in Python?

However, since the next non-empty line it reads is the elif line, and that line's parent is the def line, the if line has no children, and Python reports that it expected some indented lines. To fix this, either place at least one line of code as the if 's child, or remove the if entirely.

What is a indented block in Python?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.


2 Answers

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.

like image 126
Mark Byers Avatar answered Oct 10 '22 19:10

Mark Byers


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.

like image 36
Pramod Avatar answered Oct 10 '22 19:10

Pramod