20

Python が「予想されるインデント ブロック」エラーを出す理由がわかりません。

""" 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)
4

2 に答える 2

31

そこの関数定義の後にdocstringをインデントする必要があります(3、4行目):

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

インデント:

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

#または、代わりにコメントを使用できます。

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

また、 docstring に関する PEP 257も参照できます。

お役に立てれば!

于 2013-10-29T12:02:08.153 に答える
5

たとえば、次のようなことも経験しました。

このコードは機能せず、意図したブロック エラーが発生します。

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

ただし、 return self.title ステートメントを入力する前にタブを押すと、コードが機能します。

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

うまくいけば、これは他の人を助けるでしょう。

于 2014-06-30T11:52:44.687 に答える