Help with level, Gas Attack

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
enemyIndex = 0
# While enemyIndex is less than the length of enemies array
while len(enemy):
if enemyIndex < len(enemy):
enemy = hero.findNearestEnemy()
sumHealth(enemy) + totalHealth(enemy)
# Increment enemyIndex by 1.
enemyIndex += 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.”)

i am stuck on this level

Welcome to the discourse @millerrj21 ! This is a family-friendly place where you can hang out! :partying_face: I ask that you read this FAQ before posting. Here is how to post your code properly.

1 Like

# 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
    enemyIndex = 0
    # While enemyIndex is less than the length of enemies array
    while len(enemy):
        if enemyIndex < len(enemy):
            enemy = hero.findNearestEnemy()
    sumHealth(enemy) + totalHealth(enemy)
        # Increment enemyIndex by 1.
    enemyIndex += 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.")

This needs to be one line:

while enemyIndex < len(enemies):
     #Do something

Also, the line below is wrong, it should be related to enemyIndex

And this needs to be changed it can be incremented too:

1 Like