I don’t know whats wrong the hero either can’t see them or they return NaN or 0
Heres my code
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 enemyIndex < len(enemies):
# Add the current enemy's health to totalHealth
totalHealth = totalHealth + enemies.health
# 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)
if ogreSummaryHealth:
hero.say("Use " + ogreSummaryHealth + " grams.")
few major issues i think these are the reason you are failing
in the while loop put enemy = enemies[enemyIndex] inside as enemy isnt actually defined until then
there is no need for the if statement at the end as if takes boolean values, not numbers. Change it to
#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.")
all good?
also you can use totalHealth += enemy.health instead of totalHealth = totalHealth + enemies.health
Good try! thats actually it and there is no other gimmiks i used either like spells or abilities so you actually did pretty good even i forgot to define enemy the first time
ef 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 enemyIndex < len(enemies):
# Add the current enemy's health to totalHealth
enemy = enemies[enemyIndex
totalHealth += enemies.health
# 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.")
An error pops up saying "unexpected token: expected T_RSQB but found T_NAME while parsing trailer