-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

Doesn't the error show the line number too? It should give you a hint of where to start. (Its on lines 4-7 btw)

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 に答える
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 に答える