Please format your code as described in the FAQ. This makes it more likely that others will help you as they don’t have to do the formatting by themselves.
If I see that right I win by using (almost) the same code. I just have a check that mostHealth actually exists.
Have you tried to submit again? Submitting gives you a new random seed, possibly resulting in another outcome.
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
friend = hero.findFriends()
enemies = hero.findEnemies()
highestHp =None
bestHealth=0
for enemy in enemies:
if enemy.health >= bestHealth:
highestHp = enemy
bestHealth = enemy.health
if highestHp:
friend = hero.findFriends()
hero.command(friend, "attack", highestHp)
If someone would be so kind as to tell me how to tell the archers how to attack the highestHp enemy … i get the error that , that is an array … and i’m really really stuck
It’s because you’re trying to command an array: friend = hero.findFriends() even though you’ve defined friend instead of friends which is a bit confusing. What you need to do is make a for loop and command each individual friend in hero.findFriends()
Is that a working code? If it is, can you delete it because here on the forum we do not want posted solutions? But if you need help at this level, can you format your code as it is described here?
I think you need to change if(enemy.health >= health) { to if(enemy.health > health) { because >= means equal or greater than. So, it could think that a dead enemy was the greatest.
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
friend = hero.findFriends()
enemies = hero.findEnemies()
best = None
maxHealth = 0
for i in len(enemies):
enemy = enemies[i]
friend = friends[i]
if enemy.health >= bestHealth:
best = enemy
maxHealth = enemy.health
if best:
hero.command(friend, "attack", best)
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
friend = hero.findFriends()
enemies = hero.findEnemies()
best = None
maxHealth = 0
for i in len(enemies):
enemy = enemies[i]
friend = friends[i]
if enemy.health >= bestHealth:
best = enemy
maxHealth = enemy.health
if best:
hero.command(friend, "attack", best)
But it says that it have a error: Need an object. What the heck does that means anyway?
You can’t command the archers like this. You’ve put the command line outside the for loop. You should create another for loop to command the archers.
Danny
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
friend = hero.findFriends()
enemies = hero.findEnemies()
best = None
maxHealth = 0
for i in len(enemies):
enemy = enemies[i]
friend = friends[i]
if enemy.health >= bestHealth:
best = enemy
maxHealth = enemy.health
if best:
pass
hero.command(friend, "attack", best)