[SOLVED] Help with convenient enemy please

hi. i’m having trouble with this level. can someone help me? tell me what is wrong with my code:

def heroDefend(x, y):
    hero.moveXY(x, y)
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.moveXY(x, y)

for x in range(8, 73, 16):
    hero.moveXY(x, 22)
    peasant = hero.findNearest(hero.findFriends())
    message = peasant.message
    if message:
        words = message.split(" ")
        for i in range(len(words)):
            wordA = words[len(words)]
        if wordA:
            hero.summon(wordA)

for i in range(len(hero.built)):
    unit = hero.built[i]
    hero.command(unit, "defend", unit.pos)

while True:
    x = 72
    y = 22
    heroDefend(x, y)

The problem is with this block:

    if message:
        words = message.split(" ")
        for i in range(len(words)):
            wordA = words[len(words)]
        if wordA:
            hero.summon(wordA)

You define ‘words’ as an array of the message components…the split function divides the message in to its individual pieces. Your task is now to define the last component, as this is the unit type you need:

        # "words" is an array of words from the "message".
        # Get the last word. It's the required unit type.

(A strong suggestion here…I would highly recommend not deleting the comments, as these can help keep you on track, or focused…but 'tis only a suggestion.)

You are attempting to use a for loop to parse thru the array (not a bad idea), but since we know the array is fixed in size and we know exactly where (which element) we need to look, there is a much simpler way…and yes, it does involve using the len() method.

I will show you that method, but want to give you a chance at figuring it out for yourself, first.

1 Like

thanks! but i need to go to bed, and i have school tomorrow, so i’ll see you in about 18 hours or maybe more. bye until :clock430: (4:30 pm French time)

No worries…we’ll be here when you get back :wink:

When you are back, can you send me the code that you have at this level?

i’m back! :partying_face:
here’s my code:

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

def heroDefend(x, y):
    hero.moveXY(x, y)
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.moveXY(x, y)

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.
        for i in words:
            wordA = words[len(words)]
        # Summon the required unit type.
        if wordA:
            hero.summon(wordA)

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:
while True:
    x = 72
    y = 22
    heroDefend(x, y)

If you delete this for loop, does the code work?

@dedreous, i’ve tried to think about the suggestion you sent but i haven’t come up with any other way :cry:

no it doesn’t @AnSeDra, the same thing happened:
he didn’t summon anything

Remember that the last element of an array has the index the array’s length - 1. Does it work now?

i guess it’s kinda the same code

it worked! :partying_face:

1 Like

Well done! Thanks Andrei…that is pretty much what I was trying to convey. :slightly_smiling_face:

2 Likes

Congratulations for completing the level! :partying_face:
And try to remember this in the future.

:rofl: :rofl: :rofl: well thanks @dedreous and @AnSeDra :partying_face:

2 Likes