Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Expected an indented block" error?

Tags:

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)
like image 918
kartikeykant18 Avatar asked Oct 29 '13 11:10

kartikeykant18


People also ask

How do you fix indentation expected an indented block?

If you are using Sublime, you can select all, click on the lower right beside 'Python' and make sure you check 'Indent using spaces' and choose your Tab Width to be consistent, then Convert Indentation to Spaces to convert all tabs to spaces.

How do you fix an indented block error in Python?

To resolve expected an indented block in Python, correct the indentation of each block. Before Python runs any code in your program, it will first discover the correct parent and children of each line. Python throws an Indentation whenever it comes across a line for which it cannot define the right parent to assign.

What is the meaning of indented block?

In general, a block indent is multiple lines of text that are indented. Most programs and websites that indent text block indent the paragraph or all text following the first line, unless it is a first-line indent or hanging indent.


2 Answers

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!

like image 129
aIKid Avatar answered Sep 24 '22 14:09

aIKid


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.

like image 33
Jaky71 Avatar answered Sep 20 '22 14:09

Jaky71