In Python indentation is significant: identically indented lines of code form a block of code which is executed together.
So when you do an
if
...
else
statement, all teh lines that are executed when the condition is true must share teh same indentation:
if condition:
line 1
line 2
else:
line 3
line 4
line 5
If the condition is true, both
line 1
and
line 2
will be executed, if it isn't, then both
line3
and
line 4
will be executed.
Your code has a second indented block in the middle:
if condition:
line 1
line 2
line 5
else:
line 3
line 4
line 5
Which means that the system cannot work out what the
else
code is doing there as the
if
statement was ended by
line 5
(This is one of the stupid things about Python, and which causes the most problems: because any whitespace is taken as significant it is possible to have two adjacent lines of code that look identical but are differently indented because spaces and tabs are not the same. Make sure you use an editor that replaces tabs with spaces or it can get very, very confusing.)