Clearly, your
if
and else
statements aren't insider the loop. Lines 5 and 8 should be indented at the same level as line 4 and so on.