-3

你能帮我解决这个问题吗?

def group_iter (iterator, n=2, strict=False):

    accumulator = []
        accumulator.append(item)
        if len(accumulator) == n: 
        yield tuple(accumulator)
        accumulator = []

    if strict and len(accumulator) !=0:
        raise ValuseError("Leftover values")


print "This is count %r " % group_iter 

当我运行它时,我得到:

accumulator.append (item)
IndentationError: unexpected indent

我怎么解决这个问题?谢谢!

4

2 回答 2

0

您的代码缩进太远,请删除多余的空格以匹配accumulator = [].

def group_iter (iterator, n=2, strict=False):
    accumulator = []
    accumulator.append(item)
    if len(accumulator) == n: 
        yield tuple(accumulator)
        accumulator = []

您可能需要查看您的 Python 教程,了解何时缩进 Python 代码以及何时不缩进。

于 2012-11-07T16:18:49.720 回答
0

错误是否也显示行号?它应该给你一个从哪里开始的提示。(顺便说一句,它在第 4-7 行)

accumulator.append(item) # remove indent
if len(accumulator) == n: # remove indent
    yield tuple(accumulator) # keep indent
accumulator = [] # uncertain whether to keep or remove this base on your needs.
于 2012-11-07T16:24:42.027 回答