Python error "IndentationError: expected an indented block"

Python

I am trying to execute the following python code:

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

And I get the folloeing error:

IndentationError: expected an indented block

2
Answers

Replies

Most programming languages permit indentation, but don't enforce it. Python enforces it with an iron fist, it has caused confusion for many beginners. The error expected an indented block is probably caused by a mix of tabs and spaces and this can lead to some confusing errors. Putting in an extra space or leaving one out where it is needed will surely generate an error message . Some common causes of this error include:


* Forgetting to indent the statements within a compound statement
* Forgetting to indent the statements of a user-defined function.


The indentation can be any consistent white space . It is recommended to use 4 spaces for indentation in Python, tabulation or a different number of spaces may work, but it is also known to cause trouble at times. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on.


 

 

Python will need its code to be spaced and indented properly. You should ensure that you are not mixing the spaces and tabs. You should use either space or tabs but not both.


You need to change the below part:


if str_p == str_q:


    result = True


else:


    result = False


return result

 
 

If you want to unleash your potential in this competitive field, please visit the Python course page for more information, where you can find the Python tutorials and Python frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.