Gas attack /Python /Keeps not seeing enemies or returning 0

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

# 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

2 Likes

I used a lot of spells(thank you swap and mana blast)

1 Like

Like this? because its still not working

``````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

#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

it should be `def sumHealth(enemies):` not that

It is i just didn’t copy and paste correctly

sorry about that, but what about this should have another `]` at the end.

Yes it should but its still just saying NaN or 0

@Ashmit_Singh
no need for the if statement at the end as if takes boolean values, not numbers

``````if ogreSummaryHealth:
hero.say("Use " + ogreSummaryHealth + " grams.")
``````

an if statement takes number values:

The problem is with this part of the code:

``````totalHealth += enemies.health
``````

It should be this:

``````totalHealth += enemies[enemyIndex].health
``````

With your code, Python does not know what enemy it is talking about, so you need to have an index. Hope this helps!

1 Like

Ok sure but it didn’t work for me

Thank you it fixed it

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.