Pythonのfor-elseを心の目で読む

The Python Tutorialの4.4章「break and continue Statements, and else Clauses on Loops」のところを読んでいて,このコード

for n in range(2, 10):
     for x in range(2, n):
         if n % x == 0:
             print n, 'equals', x, '*', n/x
             break
     else:
         # loop fell through without finding a factor
         print n, 'is a prime number'

のelseのインデントの位置がおかしいのでは?と思ったんだけど,同じことを思った人もいたみたいでこれPythonのforループのelse節を冗長に読解 - zuzuPost
を読んだらわかった.確かにこのコードでやりたいことを考えると,ここにelseがないといけない.

丁寧にもTutorialのつづきには,

(Yes, this is the correct code. Look closely: the else clause belongs to the for loop, not the if statement.)

とあって,見逃さないでね〜これで正しいんだよ〜と伝えてくれている.