Gas Attack, Sarven Desert

My code doesn’t seem to work though it worked a while ago so I changed it a bunch but it still won’t work because I don’t know what variable to use.

def sumHealth(enemies):
    # Create a variable and set it to 0 to start the sum.
    totalHealth = 0
    # Initialize the loop index to 0
    index = 0
    # While index is less than the length of enemies array
    while index < len(enemies):
        # Add the current enemy's health to totalHealth
        totalHealth = enemies[index].value
        # Increment the index.
        index += 1

    return totalHealth

# Use the cannon to defeat the ogres.
cannon = hero.findNearest(hero.findFriends())
# The cannon can see through the walls.
enemies = cannon.findEnemies()
# Calculate the sum of the ogres' health.
ogreSummaryHealth = sumHealth(enemies)
hero.say("Use " + ogreSummaryHealth + " grams.")

1 Like

Units are not coins (or gems). They don’t have value only health.

1 Like

I did that and it worked but now only for one munchkin. I tried an infinite loop but it still did it only for one munchkin you would think it would do it for more.

1 Like

Fixed the problem!
20

1 Like

How? I still can’t do more than one!

1 Like

health instead of value

1 Like

Sorry, I found out.

cannon = hero.findNearest(hero.findFriends())
ogreSummaryHealth = 0
enemies = cannon.findEnemies()
ogreSummaryHealth = enemies[0].health + enemies[1].health + enemies[2].health + enemies[3].health +
enemies[4].health + enemies[5].health + enemies[6].health + enemies[7].health + enemies[8].health +
enemies[9].health
hero.say(“Use” + ogreSummaryHealth + “grams”)

1 Like