本文介绍了“预期缩进的块"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白python为什么会给出预期的缩进块"错误?

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)

推荐答案

您必须在函数定义之后缩进文档字符串(第3、4行):

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'

缩进:

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'

此外,您还可以看到 PEP 257 关于文档字符串.

Also, you can see PEP 257 about docstrings.

希望这会有所帮助!

这篇关于“预期缩进的块"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:54