簡體   English   中英

“預計縮進塊”錯誤?

[英]“Expected an indented block” error?

我無法理解為什么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)

你必須在函數定義之后縮進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'

此外,您可以看到有關文檔字符串的PEP 257

希望這可以幫助!

我也經歷過這樣的例子:

此代碼不起作用並獲得預期的塊錯誤。

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語句之前按Tab鍵時,代碼可以正常工作。

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

希望,這會有助於他人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM