Indentation error in Python














































Indentation error in Python



Indentation error in python

In python, indentation is required for identifying what block of code statement belongs to. Whitespaces is used for indentation.

There exist three types of indentation errors in python.
  • Expected an indented block
  • Unexpected indent
  • Unindent does not match any outer indentation level


1. Expected an indented block:

Example:

def wish():
print('hello')

wish()
Output:



Here, python is expecting some indentation but we are not providing proper spaces. To get rid of this error, go to 'print' line and provide proper indentation, re-run it.

2.Unexpected indent:

Example:

 def wish():
    print('hello')

wish()
Output:



Here python is not expecting any indentation but we are providing. To get rid of this, go to line1 and remove the unwanted spaces then re-run it.

3. unindent does not match any outer level indent:

Example:

def wish():
    print('hello')
   return
wish()
Output:



Here, the indent that we had provided in line3 does not match with outer indentation level(i.e., line2). There is mismatch of spaces in 'return' statement. Make sure that you are providing equal spaces throughout your program.


Comments