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.")
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.
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
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.
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.
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.")
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.