Python Help on "Fair Battle"

I believe that my code is written properly, however even when my team has more health than the enemies, they fight and still lose, please see my code below.

def healthSum(target):
targetIndex = 0
health = 0
while targetIndex < target.length:
    target = target[targetIndex]
    targetIndex +=1
    health += target.health
return health    

while True:
friends = hero.findFriends()
enemies = hero.findEnemies()
# Calculate and compare the total health of your soldiers and the ogres.
if healthSum(friends) > healthSum(enemies):
    hero.say("Attack")
# Say "Attack" when you are ready.

Try to “submit” it several times. If it’ll help, please write me here.

I’ve reformatted my code in the original question, it’s still not working D:

Do you try “run” or “submit” too?

Oh. I found it. Your code is wrong.

Instead while targetIndex < target.length: use

while targetIndex < len(target):

Python arrays don’t have length property.

1 Like

I changed it as you suggested, and it is still not working for me.

def healthSum(target):
targetIndex = 0
health = 0
while targetIndex < len(target):
    target = target[targetIndex]
    targetIndex +=1
    health += target.health
return health    

while True:
friends = hero.findFriends()
enemies = hero.findEnemies()
friendHealth = healthSum(friends)
enemyHealth = healthSum(enemies)
hero.say(friendHealth)
hero.say(enemyHealth)
# Calculate and compare the total health of your soldiers and the ogres.
if friendHealth > enemyHealth:
# Say "Attack" when you are ready.
    hero.say("Attack")

Try to remove

hero.say(friendHealth)
hero.say(enemyHealth)

Because it add “delay” and as the result in that frame if friendHealth > enemyHealth: the state can be not the same as when you calculated it.

I just followed your direction, to no avail. My guys cant even kill one ogre ;(

Ok. Please post your last code. And be careful with indents.

def healthSum(target):
    targetIndex = 0
    health = 0
while targetIndex < len(target):
    target = target[targetIndex]
    targetIndex +=1
    health += target.health
return health    

while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    friendHealth = healthSum(friends)
    enemyHealth = healthSum(enemies)
    if friendHealth > enemyHealth:
        hero.say("Attack")
def healthSum(target):
    targetIndex = 0
    health = 0
    while targetIndex &lt; len(target):
        target = target[targetIndex]
        targetIndex +=1
        health += target.health
    return health

I see the problem.

The parameter name you’re passing in is: target, however you are overwriting it inside the loop, which will cause problems. Instead rename the parameter to targets since you’re passing in an array, and update the while loop accordingly.

1 Like

You use target twice and redefine it inside while loop.

Update: Serg answered before me :slight_smile:

Thank you so much!!!

Hi, thanks for making this post in the first place, I’ve encountered the same problem somehow…

Could you help me to check if there is anything wrong with my code? Many thanks :sweat:

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

# Write a function that takes an array of units as input
# And returns the sum of all the units' health
def sumHealth(units):
    totalHealth = 0
    unitsIndex = 0
    
    while unitsIndex < len(units):
        unit = units[unitsIndex]
        if unit.health > totalHealth:
            totalHealth += unit.health
        
        unitsIndex += 1
        
        return totalHealth

while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Get the total health of your soldiers and the ogres.
    soldierHealth = sumHealth(friends)
    ogreHealth = sumHealth(enemies)
    # Say "Attack" when your side has more total health.
    if soldierHealth > ogreHealth:
        hero.say("Attack")
    pass


Do you know what that means? It means if the unit’s health is less than or equal to the totalHealth already added up, it won’t get added to the total. (just remove the if loop while keeping the totalHealth += unit.health part

1 Like

also unindent return totalHealth

1 Like

Hi SuperSmacker, thanks for helping out!:grinning:

You are right!! Now the totalHealth will accumulate correctly and I can beat the level!

You are the SuperSmacker! Happy X’mas!:star2::evergreen_tree:

1 Like

I’m not sure what I’m doing wrong but my code isn’t working. My hero keeps saying attack right after an ogre keeps showing up.

# 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(target):
    totalHealth = 0
    targetIndex = 0
    # Complete this function:
    while targetIndex < len(target):
        target = target[targetIndex]
        totalHealth += target.health
        targetIndex += 1
    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!!!")

Can someone please help. It also doesn’t help that I don’t completely understand arrays.

Look at your sumHealth function. The input for the function is supposed to be an array (so targets, not target). You got confused within the while True loop and defined target = target[targetIndex]. Switch the input of sumHealth function to targets (with an s, so you don’t confuse yourself), switch the target in len(target), and switch the target in front of [targetIndex] to targets too. Then run the code again. It should work out.

Thanks @SuperSmacker. That helped. I’ve really got to better at arrays. That has always been where I struggle in programming even when I was in college courses. I get how they work but when going through them that’s where I get lost especially when trying to understand how to set priority and stuff.

1 Like