Here is my code :
# 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 index = 0 # While index is less than the length of enemies array while index < len(enemies): # Add the current enemy's health to totalHealth totalHealth += enemies.health # Increment the index. index += 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.")
Doesn’t shows any errors, and my code looks fine to me, doesn’t work though. Everytime I run it, it just says “Use NaN grams.” It finds the sum of the enemies’ health, and then says to use that many grams, but that many grams is NaN and I don’t see any errors in the code to find the sum of their health.