Toil and Trouble - codes work but not perfect

Hi, folks

I succeeded this level but somehow got this notice while I was running the codes, please have a look, thanks :slightly_smiling_face:

Just in case you guys need the codes:

# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
def chooseTarget(friend):
    witch = friend.findNearest(hero.findByType('witch'))
    nearestEnemy = friend.findNearestEnemy()
    
    if friend.type == 'soldier':
        if witch:
            return witch
    if friend.type == 'archer':
        if nearestEnemy:
            return nearestEnemy



while True:
    
    friends = hero.findFriends()

    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        target = chooseTarget(friend)
        hero.command(friend, "attack", target)
        pass

You need to check if there’s an ally present. The array hero.findFriends() can be empty, and you need an if statement to see if an ally actually exists.

I just put in an “if” statement to confirm the existence of the friends, but got the same result:

You need if friend: on line 24.

still not working :sweat_smile:

You need to indent line 25. It’s blocking the code from executing.

that line is already commented out, so I guess it shouldn’t affect the execution. and I just indented line25, but still not working, I guess the codes in the is quite suspicious, but couldn’t figure out where that is

the codes in the function chooseTarget(friend)

After line 26, check if there’s a target using if target:

This issue is still not solved yet, any idea folks? :blush:

Screenshot of error please.

it’s always the same error, you can refer to the ones that I’ve posted

@xiaoming did you do what I told you to do?

I checked the target, there are 2 if statements in the “chooseTarget(friend)” function

Can you post the new code please?

yeah, sure, thanks mate

# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
def chooseTarget(friend):
    witch = friend.findNearest(hero.findByType('witch'))
    nearestEnemy = friend.findNearestEnemy()
    
    if friend.type == 'soldier':
        if witch:
            return witch
    if friend.type == 'archer':
        if nearestEnemy:
            return nearestEnemy



while True:
    
    friends = hero.findFriends()

    for friend in friends:
        if friend:
            # Use your chooseTarget function to decide what to attack.
            target = chooseTarget(friend)
            hero.command(friend, "attack", target)
            pass

You never checked if there’s a target before hero.command(friend,"attack",target)

it’s in the def function, “if witch” and ‘if nearestEnemy’, it’s not right to put it there?

Nope, you still have to put if target: after hero.command(friend,"attack",target).

1 Like