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

Python Fair Battle Level Help

# Attack when your soldiers' total health is greater
# than the ogres' total health

# This function return the sum of all the units' health.
def sumHealth(units):
    totalHealth = 0
    # Complete this function:
    
    return totalHealth

while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Get the total health of your soldiers and the ogres.
    if sumHealth(friends) <= sumHealth(enemies):
        hero.say("Wait")
    # Say "Attack" when your side has more total health.
    else:
        hero.say("ATTACK!!!")

Help me my code is not working

Hi Everncast and welcome to the forum! You didn’t complete the sumHealth function.

What is the sumHealth function? I don’t think I know that.

This is the function you should complete.

Thank you for helping me with that

Do you need any more help?

# Attack when your soldiers' total health is greater
# than the ogres' total health

# This function return the sum of all the units' health.
def sumHealth(units):
    totalHealth = 0
    # Complete this function:
    sum(totalHealth)
    return totalHealth

while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Get the total health of your soldiers and the ogres.
    if sumHealth(friends) <= sumHealth(enemies):
        hero.say("Wait")
    # Say "Attack" when your side has more total health.
    else:
        hero.say("ATTACK!!!")

I keep losing the battle and I don’t know why.

Your totalHealth should be the sum of the healths of the units in the units array.

1 Like

so would it be

totalHealth = sum(friends)

No. You should put:

    for unit in units:
            totalHealth += unit.health

instead of this:

Thank you so much @AnseDra for helping me with this it worked now :crocodile:

No problem :wink: :slight_smile: