Gas Attack - codecombat [ SOLVED ]

Hy guy, please, i need help fo this level… there is my code…

# Calcolare la somma totale della vita degli ogres

def sumHealth(enemies):
    totalHealth = 0         # Creo Varibile e la faccio partire a contare da 0
    enemyIndex = 0          # Punto di partenza dell'array
    # While enemyIndex is less than the length of enemies array
    while enemyIndex <len(enemies):
        health = totalHealth[enemyIndex]
        # Add the current enemy's health to totalHealth
        totalHealth +=health   # is this the problem?
        # Increment enemyIndex by 1.
        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.")

I don’t know if I’m right, but I don’t think totalHealth is a list or tuple…

1 Like

The main part that needs some adjustment is:

        health = totalHealth[enemyIndex]

Let me break this down a bit.

You have an array (list) of enemies.
You want to run through each enemy in that list using the enemyIndex.
enemies[enemyIndex]
As you go through each enemy, get the health of each enemy with .health and add to totalHealth.

There are a couple of ways to do this: assign the different parts to variables, or in one line collect all the info and add it to totalHealth.

1 Like

Ok thanks brooksy… i correct the code :slight_smile:

 while enemyIndex <len(enemies):
        enemy = enemies[enemyIndex] # scorro lista dei nemici
        health = enemy.health       # assegno a health la vita di ogni nemico della lista.
        totalHealth += health       # aggiungo .health a totalHealth
        enemyIndex = enemyIndex+1   # scorro  la lista di 1
    return totalHealth

now works fine… but there another way to solve? thanks :slight_smile:

1 Like

It isn’t necessarily another way to solve the problem, but condensing the code. Listing out the variables like you did helps you see each step. When you understand how all the parts work, you can skip the variables and grab the information in one line.

Variable vs Condensed code
enemy = enemies[enemyIndex] 
health = enemy.health      
totalHealth += health 

Can be condensed to:

totalHealth += enemies[enemyIndex].health

Essentially it minimizes the code you write and can make it look a little cleaner.

but there another way to solve?
For an idea how to solve it see Rational Defense
Picture of the solution:


and the corresponding video

True, there are usually several ways you can complete most levels with the goals provided. Although, this usually requires writing code that isn’t in line with the starter code. Not something I would suggest as a beginner, but after you have completed the levels and are looking for some more practice; circle back on some of these levels, delete all the starter code and start from scratch trying to complete the level in another way.

It is a great way to test you understanding and develop creative problem solving solutions in code.

I find another way…
Thanks for suggestion…

def sumHealth(enemies):
    totalHealth = 0         # Creo Varibile e la faccio partire a contare da 0
    enemyIndex = 0          # Punto di partenza dell'array
    
    # Scorro l'array enemies per tutta la sua lunghezza
    while enemyIndex <len(enemies):
        totalHealth += enemies[enemyIndex].health # scorro lista dei nemici, controllo e sommo health 
        enemyIndex = enemyIndex+1   # incremento di 1 enemyIndex
    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.")

Hy xython, thanks for answer, but how is possible to build fire trap whit paesant?

You need a Boss Star 2 which you might not have yet to command peasants and Boss Star 3 to summon them. You start getting the Boss Stars in Cloudrip Mountain. Thanks.

1 Like

Ok thanks :slight_smile:

it not working help please!!!

how do you get boss star V?

:grin::grin::grin:

If you need help with your code, please post it (formatted as it says in this topic) and state your problem more clearly.
Thanks
Danny

It’s not currently released for anyone (except Nick of course :wink:).

I do and I don’t get how to command

sad becuase when you can spawn a artillery you can make a fortress with cannons LOL

pls halp
i am stuck it has been long time since i tried

Please could you post your current code.
Danny