[SOLVED] Toil and trouble....certainly toil for me!

That method is not available for friends so you’ll have to use hero there, but change the rest to friend in the definitions. friend.FindNearest(hero.findByType(“witch”))

12

45

witch = friend.FindNearest(hero.findByType(“witch”))
enemy = friend.FindNearestEnemy()

The parameter, friend, is missing where you call the function in the while True loop in the enemy definition.

1 Like

massive thanks! :grinning::blush:

1 Like

I managed to get the targeting function do what the game asks but it still fails, advice?

Hi @Determined_heli, welcome to the codecombat discourse.
If you need help, please could you post your code. (formatted: instructions)
Thanks

I know that this topic is old but I am having difficulty similar to theirs and can’t get my people to go after any of the witches.

# Ogre Witches have some unpleasant surprises ready for you
def chooseTarget(friend):
    
    
    witch = friend.findNearest(hero.findByType("witch"))
    enemy = friend.findNearestEnemy()
    if friend and friend.type == "soldier":
        return witch
    if friend and friend.type == "archer":
        return enemy 
# 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.
while True:
    friends = hero.findFriends()
    for i in friends:
        friend = friends[i]
        enemy = chooseTarget()
        if enemy:
            hero.command(friend, "attack", enemy)

Well, you are defining your function to accept one argument, but when you call the function, you are not passing anything to it.

1 Like

While it is preferred not to necropost, it is not a bad thing either…certainly better than having several topics about the same question/issue.

1 Like

I don’t understand what you are saying could you try rewording that?

When you defined your chooseTarget function, you specified an argument:

named ‘friend’. This tells the function to use this object as a variable in the function’s code.

But, when you actually call the function:

you are not providing an object for the function to work with. This is called passing…think of it as ‘sending’ the object (in this case your ‘friend’ variable) to the function so that it can do its magic. It’s this second part that needs adjusting.

Ok I set the thing to friend but the code keeps doing the same thing. Any suggestions?

Please could you post your new code. It’s always good practice to do this in case you haven’t followed the instructions exactly. (No offence, it’s just that everyone can make mistakes.)
Danny


    # Ogre Witches have some unpleasant surprises ready for you
def chooseTarget(friend):
    
    
    witch = friend.findNearest(hero.findByType("witch"))
    enemy = friend.findNearestEnemy()
    if friend and friend.type == "soldier":
        return witch
    if friend and friend.type == "archer":
        return enemy 
# 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.
while True:
    friends = hero.findFriends()
    for i in friends:
        friend = friends[i]
        enemy = chooseTarget(friend)
        if enemy:
            hero.command(friend, "attack", enemy)

The problem is with the for loop:

for i in friends:
    friend = friends[i]

I think you should be able to see this is wrong. If not look over the previous mountain for-loop levels.
There are two correct ways of doing a for loop:

for thing in things:
    do action--
# or
for i in range(0, len(things)):
    thing = things[i]
    do action--

You’re a bit confused between the two.
Danny

it is still not working do I need to put something other than a 0 or is

for i in range(0, len(friends))

fine

here is the code

def chooseTarget(friend):
    
    
    witch = friend.findNearest(hero.findByType("witch"))
    enemy = friend.findNearestEnemy()
    if friend and friend.type == "soldier":
        return witch
    if friend and friend.type == "archer":
        return enemy 
# 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.
while True:
    friends = hero.findFriends()
    for i in range(0, len(friends)):
        friend = friends[i]
        enemy = chooseTarget(friend)
        if enemy:
            hero.command(friend, "attack", enemy)

NVM Iused a weird code at the top so I was not using that code I used it and won.
Thanks to everyone who helped me!

I am having trouble too here is my code

# Ogre Witches have some unpleasant surprises ready for you
def chooseTarget(friend):
    
    enemy = hero.findNearestEnemy()
    witch = hero.findNearest(hero.findByType("witch"))
    if friend and friend.type == soldier:
        return witch
    if friend and friend.type == archer:
        return enemy 
# 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.
while True:
    friends = hero.findFriends()
    for friend in friends:
        enemy = chooseTarget()
        if enemy:
            hero.command(friend, "attack", enemy)
        
       
        pass