Hellenar is correct - adding to that is the fact that there is no check for an enemy before it so if there is a point that there is no flag and no enemy your code could fail.
Thanks for your help. I erased that line and run the code again, Anya lived a little bit longer but not enough ,maybe I should buy for her different type of equipment . Maybe 2100 it’s less then she need
Can you post a screen shot of the equipment you are using?
Also, one of the things you’re supposed to use the flags for is to maneuver your hero to the left of the archers so that they attack anyone who comes through the gates and you take less damage. If you’re taking as much damage as you say, try staying out of the fight and letting the soldiers and archers do more. Use your flags so that you only step in and fight when when needed.
Not gonna lie was kinda proud first time beating this lvl. I had the same issue.
For me the trick was to only attack in case I planted a flag. You need to lure the enemies in front of the archers so you can deal with the backline casters. If you attack all enemies in sight you will indeed run out of time.
I also used multiple flag colours to make sure I could move without attacking anything
Mod edit: Please do not post solutions on this board. The purpose of the board is to help people write their own code and providing final solutions is counter productive to the learning process.