Convenient Enemy Help!

Hello! my code is fine, it works but my hero just dosent atack at the end. Can anybody help me?

# Ogres are hiding in woods. Protect the peasants.
# The last word in the peasants' messages are a hint.

for x in range(8, 73, 16):
    hero.moveXY(x, 22)
    # Peasants know whom to summon.
    peasant = hero.findNearest(hero.findFriends())
    message = peasant.message
    if message:
        # Words are seaparated by whitespaces.
        words = message.split(" ")
        # "words" is an array of words from the "message".
        # Get the last word. It's the required unit type.
        type = words[words.length - 1]
        # Summon the required unit type.
        hero.summon(type)

for i in range(len(hero.built)):
    unit = hero.built[i]
    # Command the unit to defend the unit's position.
    hero.command(unit, "defend", unit.pos)
# Defend the last point yourself:
enemy = hero.findNearestEnemy()
if hero.findNearestEnemy():
    hero.attack(enemy)
    

Think about when this happens, is there an enemy at that point? And, more importantly, how many times will this happen.
Remember: in normal code, when there are no loops, everything happens only once.
I hope this helps.
Danny

So your saying i should put the code in a loop?

Well, think about it. As @Deadpool198 said,

If there aren’t loops, things only happen once. Now take another look at your code:

There aren’t any loops, so the hero only will attack once.

_

_

_

_

_
(Btw I think this part needs to be fixed:)

(Could be only me)

solved it (all i did was put a loop in the atack part!
Like so…

while True:
     
    for i in range(len(hero.built)):
        unit = hero.built[i]
        # Command the unit to defend the unit's position.
        hero.command(unit, "defend", unit.pos)
    # Defend the last point yourself:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    

Nice. (20 :rice_ball: :rice_ball: :rice_ball:s)