21

I can't understand why python gives an "Expected indentation block" error?

""" This module prints all the items within a list"""
def print_lol(the_list):
""" The following for loop iterates over every item in the list and checks whether
the list item is another list or not. in case the list item is another list it recalls the function else it prints the ist item"""

    for each_item in the_list:
        if isinstance(each_item, list):
            print_lol(each_item)
        else:
            print(each_item)
aIKid
  • 26,968
  • 4
  • 39
  • 65
kartikeykant18
  • 1,737
  • 6
  • 28
  • 42
  • 1
    Umm... why a picture of text? It would make more sense to just post the actual code... It's not that much after all :) Would have probably been faster too. – Lix Oct 29 '13 at 11:58
  • 14
    You have to indent the docstring in line 3. – tobias_k Oct 29 '13 at 11:58
  • 1
    If you can, please consider pasting the code here instead of posting an image. – aIKid Oct 29 '13 at 11:59
  • n-thing the notion that screenshots of code are silly. Also, the *reason* you have to indent docstrings is that they're **not** comments. They're actual string objects that get attached to the module/class/function they're for (as the `__doc__` attribute) by the parser, so they have to be in their place in the parse tree. – millimoose Oct 29 '13 at 12:00
  • 2
    I'm darn curious to know what editor you're using that makes taking a screen shot and then cropping it simpler than copy and paste :) – Jon Clements Oct 29 '13 at 12:01
  • I think i tried that on my first question here too, Jon. I couldn't figure out how to format my code XD – aIKid Oct 29 '13 at 12:03

2 Answers2

31

You have to indent the docstring after the function definition there (line 3, 4):

def print_lol(the_list):
"""this doesn't works"""
    print 'Ain't happening'

Indented:

def print_lol(the_list):
    """this works!"""
    print 'Aaaand it's happening'

Or you can use # to comment instead:

def print_lol(the_list):
#this works, too!
    print 'Hohoho'

Also, you can see PEP 257 about docstrings.

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • 2
    Just a tip. When you are using a cmd window to execute python line by line, it'll show a `...` at the beginning of the second line. You still have to press the `Tab` key before inputting everything to avoid this error. – John Hany Mar 31 '16 at 07:06
5

I also experienced that for example:

This code doesnt work and get the intended block error.

class Foo(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField()

def __unicode__(self):
return self.title

However, when i press tab before typing return self.title statement, the code works.

class Foo(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField()

def __unicode__(self):
    return self.title

Hope, this will help others.

Jaky71
  • 162
  • 1
  • 9