Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

What is unexpected indent in Python?

When I run the following code, I am getting an error as follows.

def a():
  print "foo"
  print "baz"

OUTPUT

print "baz"
^
IndentationError: unexpected indent

How to address this problem?


1 Answer
Manogna

Python not only insists on indentation, it insists on consistent indentation. If we indent one line by 4 spaces, but then if we indent the next by 3 (or 5, 6, .), we get this error of unexpected indent in python.

In the given code, line 3 has more spaces at the start than line 2. All lines of code in a block must start with exactly the same number of spaces. Both print statements must be indented same number of spaces. So the corrected code that does not show unexpected indent is as follows.

def a():
    print "foo"
    print "baz"

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.