Gas Attack Error - No Idea What It Means

Just started working on this level again. First started it back in July and still can’t figure out what the heck is wrong. Everything I do just produces more bright red errors that don’t make any sense to me.

I have no idea what this error means. Any help is appreciated. I’ve really been struggling with this for a while. Thanks.

This is my current code. It still produces the same error as in above screenshot.


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

Line 11.

You’re trying to add enemies[totalHealth] to enemy.health. There are a few reasons this is wrong:

  1. Trying to modify an enemy’s health. That’s cheating!
  2. Enemies[totalHealth] is an enemy, (in fact, it’s always the first enemy as totalHealth never changes from 0). Not a number.
  3. You aren’t doing what the comment says. You need to add the enemy’s health to totalHealth.

Cheating? Cheating is usually something people do to take shortcuts and gain an undeserved benefit. All I got was a headache from frustration. :confounded:

I switched the two around and it now works but I have no idea why. Please explain.

Thank you.

enemy.health += enemies[totalHealth]

is saying:

healthNumber = enemies[totalHealth]
# Note that healthNumber isn't actually a number!
# It is an enemy unit.
# Because enemies is an array of units.
enemy.health += healthNumber # You're trying to add a unit to a number!

You’re trying to “cheat” because you’re trying to modify an enemy’s health. The game would be really easy if I could do:

enemy = hero.findNearestEnemy()
enemy.health = 0

Be careful to read what you’re coding. You cannot add a unit to a number! You also cannot modify a unit’s properties (like health).