Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Level:borrowed sword help pls

Hi @zython and welcome to the forum! :partying_face:

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?

Andrei

nope, idk ???

I don’t know if that was right or wrong, I just guess.

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.

Please do not revive dead topics unless you have a similar issue.

Andrei

you change for enemy in enemies: to for i in enemies: then, you add enemy = enemies[i]
and friend = friends[i].

# 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)


does this work?

I tried this

# 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?

What about you maxHealth variable.

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

1 Like

it still doesn’t work:

# 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)

No, he does not. He said to command each archer to attack individualy.

Andrei

hey what is going on?

how can I do it anyways?

best = None 
bestHealth = 0
while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies() 
    while True:
        for x in len(friends):
            friends = hero.findFriends()
            friend = friends[x]
    for i in len(enemies):
        enemies = hero.findEnemies()  
        enemy = enemies[i] 
        if enemy.health >= bestHealth: 
            best = enemy 
            bestHealth = enemy.health 
    if best:
        for friend in friends:
            hero.command(friend, "attack", best)              

Can you send me a link to the level to refresh my memory with it?

Andrei

1 Like

Why did you delete the comments as they are meant to guide you to the solution? (PS I lost my subscription so without the comments I will not be able to help you unfourtunally :cry:)

Andrei

Do you mean this?

#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
    bestHealth=0
    for i in enemies:
        enemy = enemies[i]
        if enemy.health >= bestHealth:
        best = enemy
        bestHealth = enemy.health

    if best:
        friend = hero.findFriends()
        hero.command(friend, "attack", best)

Yes, but I do not remember the level too well unfourtunaly so I can not help you much. :crying_cat_face:

Andrei

okay, I’ve solved it. Thx

1 Like

Then congratulations for completing the level! :partying_face:

Andrei