Python error "IndentationError: expected an indented block"

0 votes

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
Jun 17, 2019 in Python by Rhea
67,087 views

3 answers to this question.

0 votes

Python requires its code to be indented and spaced properly. Never mix tabs and spaces. If you're using tabs stick to tabs and if you're using space stick to space.

Also in your code, change this part:

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

To

return str_p == str_q
answered Jun 17, 2019 by Varun

edited Jun 17, 2019
0 votes

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 error message IndentationError: expected an indented block would seem to indicate that you have an indentation error. It is probably caused by a mix of tabs and spaces. 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. Tabs are a bad idea because they may create different amount if spacing in different editors . 

answered Mar 2 by evanbung
• 180 points
0 votes
IndentationError:  what is the mean
answered Oct 14 by anonymous
• 140 points

Hello,

For your query you can refer this:https://www.edureka.co/community/80421/indentationerror-expected-an-indented-block

Hope it helps you!!

Thank You!!

Hi, @There,

Whenever you have a situation with code inside of a statement (such as the code that defines the main() function), that 'inside' code must be indented, and must be indented consistently. If you forget to indent, you'll see this error.

Related Questions In Python

0 votes
1 answer

I was written the python program, am getting error message "IndentationError: expected an indented block"

Hi@Thiyagarajan, You have one else block in your ...READ MORE

answered May 27 in Python by MD
• 68,280 points
1,309 views
0 votes
1 answer

I got expected an indented block error.

Hi, You used one function in your code, ...READ MORE

answered Jun 16 in Python by MD
• 68,280 points
725 views
0 votes
1 answer

IndentationError: expected an indented block

Hello @ nishit, Indentation means the space from margin to ...READ MORE

answered Aug 9 in Python by Niroj
• 68,780 points
574 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,480 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,112 views
0 votes
1 answer

Python error "TypeError: range() integer end argument expected, got numpy.float64."

Hi @Kashish, try something like this: for i ...READ MORE

answered May 27, 2019 in Python by Harish
870 views
0 votes
1 answer

How can I raise an exception in Python so that it can later be caught via an except block?

It's pretty simple to raise a query  raise ...READ MORE

answered May 29, 2019 in Python by Umesh
151 views