Indentations again!
Your if distance < 5:
is not indented relative to your if enemy:
. This means that you potentially attack enemies that are not actually present.
This is of course unless your Copy-Paste missed some spaces again.
Regarding loops:
I wrote a longer post here
http://discourse.codecombat.com/t/shine-getter-javascript/2542/4?u=j_f_b_m