Python “期望缩进块”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19657576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Expected an indented block" error?
提问by kartikeykant18
I can't understand why python gives an "Expected indentation 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)
采纳答案by aIKid
You have to indent the docstring after the function definition there (line 3, 4):
您必须在函数定义之后缩进文档字符串(第 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 257about docstrings.
此外,您可以查看有关文档字符串的PEP 257。
Hope this helps!
希望这可以帮助!
回答by Jaky71
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.
但是,当我在输入 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
Hope, this will help others.
希望,这会帮助其他人。