Gas Attack [Help wanted]

Here is my code :

# Calculate the total health of all the ogres.

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.health
        # 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.")

Doesn’t shows any errors, and my code looks fine to me, doesn’t work though. Everytime I run it, it just says “Use NaN grams.” It finds the sum of the enemies’ health, and then says to use that many grams, but that many grams is NaN and I don’t see any errors in the code to find the sum of their health.

You can’t just add up the health of all of the enemies at once you need to add each enemy separately one by one. Try doing that and come back if there is still problems.

How would I go by doing this exactly? The whole arrays thing has been screwing me over recently because I don’t fully understand how to use it.

Have you played levels before that about “loop through arrays”?

while enemyIndex < len(enemies):
    currentEnemy = enemies[enemyIndex]
    ...

Yeah but it was more of just squeezing past and getting the levels done without completely understanding what I as doing. ever since the array levels started I’ve sort of been confused with it.

Also, with arrays and later on also with %'s, I feel like the game sort of just throws it at you and hopes that you understand everything. This is my first experience with coding other than basic Html/css and I’ve gotten really confused with both.

Shouldn’t my above code add them one at a time? It should loop through each enemy and add their health to the totalHealth variable. I don’t understand why it isn’t doing this or why it doesn’t work.

EDIT : After having played around with it for a bit, I got it to work. Not sure why it wasn’t working earlier, but it’s probably just because I’m new to this and it was a rookie mistake. All I had to do is add currentEnemy = enemies[enemyIndex] like Bryukh did and then use the currentEnemy.health variable and add that to totalHealth instead of just adding enemies.health // Thank you to Bryukh and FlowerChi for helping out.