I literally can't pass "Borrowed Sword" level 36 - (CS4) [SOLVED]

This is my code but I can’t defeat the last 2 yeti’s and I have a code error saying " Argument Error: hero needs something to command"

def findStrongestEnemy(enemies):
    enemyIndex = 0
    strongest = None
    strongestHealth = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > strongestHealth:
            strongest = enemy
            strongestHealth = enemy.health
        enemyIndex += 1
    return strongest

while True:
    enemies = hero.findEnemies()
    target = findStrongestEnemy(enemies)
    friends = hero.findFriends()
    friendIndex = 0
    attackers = friends[friendIndex]
    if target:
        friendIndex += 1
        hero.command(attackers, "attack", target)

What do I do? Mind you I have been struggling on the last 5 levels of cs4 (30 - 35)
:sob:

Hi @Tyler_Reed try using a for loop to loop through all the enemies. If the enemy’s health is greater than greatest health, make the enemies health greatest health and the strongest = enemy. Then have the archers attack the strongest.

OK, I updated the code according to your instruction @iggymaster99 but the problem is that I don’t fully understand the for loop operator Can you explain how to use it and also check to verify I did this correctly?

def findStrongestEnemy(enemies):
    enemyIndex = 0
    strongest = None
    strongestHealth = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > strongestHealth:
            greatestHealth = enemy.health
            strongest = enemy
        enemyIndex += 1
    return strongestEnemy
    
    
for enemy in enemies:
    enemies = hero.findEnemies()
    target = findStrongestEnemy(enemies)
    friends = hero.findFriends()
    friendIndex = 0
    attackers = friends[friendIndex]
    if target:
        friendIndex += 1
        hero.command(attackers, "attack", strongest)

1 Like

Some of your variables are mistyped, greatestHealth = enemy.health instead needs to be
strongestHealth = enemy.health, and
return strongestEnemy needs to be return strongest

In python, a for loop will run once for each item in an array:
For example, in a fictional array called units with the elements: ["archer", "soldier", "ogre"]
The unit in for unit in units: will become “archer”, then “soldier”, then “ogre” on the first, second, and third iterations of the for loop, and you can use the “unit” variable to find unit’s type, damage, maxHealth, etc.
For your code in this level to work, you can either use a for loop or a while loop to command the archers:
If you want to use a for loop, you don’t need friendIndex, just find the strongest enemy and then loop over the friends using for friend in friends and command them to attack the strongest enemy.

If you want to use a while loop, find the strongest enemy and do the same thing that is in the findStrongestEnemy function but to command your archers.
Sorry if that’s a mouthful; I don’t remember if the levels before this explain for loops, but if they do you should revisit them. Otherwise the while loop thing works fine.

You are all good! I am just trying to process it all because it got a whole lot confusing once they started using for loops and “range” but I think I get what you are saying it is just hard for me to put into practice but I will provide more updates based on my progress! Thank you @JustALuke

1 Like

Glad I could help! :heart:
I tend to think of using range as another type of loop, because for i in range(10) will return numbers from 0-10 instead of elements in an array, such as in for unit in units, which will just return each unit.

Yeah that makes a lot of sense! but yes your help is much appreciated this is my updated code.
as of now I only got rid of the “enemyIndex” variables in the defined function findStrongestEnemy() an now I wonder if I should be getting rid of the “friendIndex” variables as well because I don’t see the need for them now, right?

def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > strongestHealth:
            strongestHealth = enemy.health
            strongest = enemy
    return strongest
# I deleted the enemyIndex variable's in order to use the "for loop" ^

for friend in friends:
    enemies = hero.findEnemies()
    target = findStrongestEnemy(enemies)
    friends = hero.findFriends()
    friendIndex = 0
    attackers = friends[friendIndex]
    if target:
        friendIndex += 1
        hero.command(attackers, "attack", strongest)

If you want to switch to a for loop, just replace the while enemyIndex < len(enemies): with for enemy in enemies and remove the statement that was previously defining the variable “enemy”. But if you want to use a while loop in the function you should retain the enemyIndex.

For the part down below, you can delete the friendIndex, but all of the code should be encompassed inside a while loop so once the archers kill one target or its no longer the strongest, they will find the new strongest enemy and attack it.

OK, one second I will fix that!

1 Like

Okay this is looking better but same outcome but it is also displaying a variable not defined error, but I am not sure what to put at “findStrongestEnemies(enemies)”

def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    eneemyIndex = 0
    for enemy in enemies:
        if enemy.health > strongestHealth:
            strongestHealth = enemy.health
            strongest = enemy
        enemyIndex += 1
    return strongest
# I deleted the enemyIndex variable's in order to use the "for loop" ^

while True:
    findStrongestEnemy(enemies)
    friend = hero.findFriends()
    if strongest:
        hero.command(friend, "attack", strongest)

Sorry, I need to be less confusing :laughing:, If you want to use a while loop, which it looks like you don’t for the function up top, you can remove the enemyIndex variables:

def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    for enemy in enemies:
        if enemy.health > strongestHealth:
            strongestHealth = enemy.health
            strongest = enemy
    return strongest

For the while loop down bottom, just put the

if strongest:
    hero.command(friend, "attack", strongest)

inside a for loop that is looping over your friends; like for friend in friends:
Also just add an s to friend in friend = hero.findFriends() to use for the for loop.

If you use while loop anywhere to loop over an array, you will need an index variable to determine which element in the array it is currently running for.
A for loop has these built in, so it doesn’t need any index variables.

ok, I did what you said lol.
Does it look better?

def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    for enemy in enemies:
        if enemy.health > strongestHealth:
            strongestHealth = enemy.health
            strongest = enemy
    return strongest

friends = hero.findFriends()
for friend in friends:
      if strongest:
          hero.command(friend, "attack", strongest)


The only issue that I think would fix the “if strongest” would be to include the defined function right?
like this:

for friend in friends:
    findStrongestEnemy(enemies)
       if strongest:
            hero.command(friend, "attack", strongest)

Nice, the top section looks perfect.

That is absolutely right and you do need to add the defined function to the for loop.
However, function won’t actually change the variable on its own; so you need to add strongest = to it to make strongest = findStrongestEnemy(enemies) as well.

After that, everything in the bottom would work, but it needs to run continuously, so incase the whole bottom section in a while (true) loop.

1 Like

Awesome, I am finally learning lol :joy:

def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    for enemy in enemies:
        if enemy.health > strongestHealth:
            strongestHealth = enemy.health
            strongest = enemy
    return strongest

friends = hero.findFriends()
for friend in friends:
    strongest = findStrongestEnemy(enemies)
    if strongest:
        hero.command(friend, "attack", strongest)

Now it says “enemies” on line that I just added. :expressionless: is undefined

Oh that’s simple to fix; just add enemies = hero.findEnemies() before the
strongest = findStrongestEnemy(enemies)

Ok those archers did their job finally! killing all the strongest ones, but then of course two giant ogres left the pack and beat up my archers lol. Do I have to change my for-loop? or put instead an else statement for the remaining enemies?

The archers are just killing the first “strongest” enemy and then stopping, so

Like this:

while True:
    friends = hero.findFriends()
    for friend in friends:
        enemies = hero.findEnemies()
        strongest = findStrongestEnemy(enemies)
        if strongest:
            hero.command(friend, "attack", strongest)

THANK YOU @JustALuke !!!
YOU ARE AMAZING!! lol I finally completed the level.

Congratulations! :tada: Glad to see it!