Something wrong with your ending %endfor
tag, there should be two.
Code between if tags will be output, <% blah %>
then code will be executed.
% for item in ('apple', 'banana'):
<%
isBanana = False
%>
% if item == 'banana':
<%
isBanana = True
%>
%endif
% if isBanana:
<span> Bought a banana</span>
%endif
%endfor