@CantMelee (CantMelee) posted a new topic, subject "Gas Attack [Help wanted]". It read:

   Here is my code :
    [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.")
        
        
    [/code]
    
    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.

Then @FlowerChi (Chi Nguyen) said

    You can't just add up the health of all of the enemies at once you need to add each enemy separately one by one. Try doing that and come back if there is still problems.

Then @CantMelee (CantMelee) said

    How would I go by doing this exactly? The whole arrays thing has been screwing me over recently because I don't fully understand how to use it.

So @Bryukh (Valentin Bryukhanov) replied

    Have you played levels before that about “loop through arrays”?
    [code] 
        while enemyIndex < len(enemies):
            currentEnemy = enemies[enemyIndex]
            ...
        
    [/code]

After which, @CantMelee (CantMelee) added

    Yeah but it was more of just squeezing past and getting the levels done without completely understanding what I as doing. ever since the array levels started I've sort of been confused with it.
    
    Also, with arrays and later on also with %'s, I feel like the game sort of just throws it at you and hopes that you understand everything. This is my first experience with coding other than basic Html/css and I've gotten really confused with both.
    
    Shouldn't my above code add them one at a time? It should loop through each enemy and add their health to the totalHealth variable. I don't understand why it isn't doing this or why it doesn't work.
    
    EDIT : After having played around with it for a bit, I got it to work. Not sure why it wasn't working earlier, but it's probably just because I'm new to this and it was a rookie mistake. All I had to do is add currentEnemy = enemies[enemyIndex] like Bryukh did and then use the currentEnemy.health variable and add that to totalHealth instead of just adding enemies.health // Thank you to Bryukh and FlowerChi for helping out.


{'source': 'discourse', 'url': 'https://discourse.codecombat.com/t/gas-attack-help-wanted/10625', 'title': 'Gas Attack [Help wanted]', 'views': 1691, 'has_accepted_answer': False, 'word_count': 468, 'like_count': 0, 'reply_count': 3, 'participant_count': 3, 'posts': 5, 'tags': '', 'author': '@CantMelee (CantMelee)', 'participants': '@CantMelee (CantMelee), @FlowerChi (Chi Nguyen), @Bryukh (Valentin Bryukhanov)', 'image_url': [None], 'created_at': '2017-02-19T21:57:13.335Z', 'last_posted_at': '2017-02-20T20:21:52.494Z'}