2012-11-07 4 views
-3

이 문제를 해결할 수 있습니까? 나는 그것을 실행할 때예기치 않은 들여 쓰기 - Python def

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 

가 어떻게이 문제를 해결할 수 있습니까? 감사!

+0

'if len (accumulator) == n :'외에도 들여 쓰기가 없습니다. –

+0

서식이 잘못되었습니다. –

+0

'accumulator = []'뒤에 코드를 들여 쓸 이유가 없습니다. – Nicolas

답변

0

들여 쓰기 코드가 너무 먼 경우 들여 쓰기를 accumulator = []의 들여 쓰기와 일치하도록 제거하십시오.

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

파이썬 코드를 들여 쓰는시기와 그렇지 않은 경우에 대한 파이썬 자습서를 검토 할 수 있습니다.

0

오류가 줄 번호도 표시하지 않습니까? 시작해야 할 부분에 대한 힌트를 제공해야합니다. (그 라인에 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. 
관련 문제