# 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
enemies = cannon.findEnemies()
# Add the current enemy's health to totalHealth
while enemyIndex < len(enemies):
# Increment enemyIndex by 1.
totalHealth += enemies[enemyIndex]
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.")
First, try to put this outside of the function
thanks! it is outside of the function
Change this to this:
TotalHealth += enemies[enemyIndex].health
But the totalhealth variable all have lower case t.
Yes, sorry I had to type it manually so the thing automatically changed it to a capital T