# Sarven Shepherd Analysis and Help

Hey everyone!

I’m new to coding, and love learning, but I’m really struggling understanding:

a) how my code below functions. what are the mechanisms of iterating over arrays. what does that have to do with the ogres and bulls.
b) why my hero isn’t doing what I want him to do (attack the ogres)

Could someone please shed some light?

Cheers!

return #Commented out to stop infinite loop.

# Use while loops to pick out the ogre

while True:
enemies = hero.findEnemies()
enemyIndex = 0

``````# Wrap this logic in a while loop to attack all enemies.
# Find the array's length with:  len(enemies)
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
# "!=" means "not equal to."
if enemy.type != "sand-yak":
# While the enemy's health is greater than 0, attack it!
while enemy.health > 0:
enemyIndex += 1
hero.attack(enemy)
pass

# Between waves, move back to the center.
``````

hero.moveXY(36, 32)

@Emm_S I find it useful to sit down sometimes and hand trace through a code segment. You can always print out the code and then keep track of each variable as you run through the loop.

For instance you can write down `enemyIndex`, `enemy.type`, and `enemy.health` and write new values for them when they change in the loop.

It is important to know when things change and why.

``````hero.say(enemyIndex + " - " + enemy.type + " : " + enemy.health );
``````

Can help you debug your code as the loop runs to understand better where things might be going wrong.

Try putting it inside of this loop: `while enemy.health > 0:`

Hi! Your “enemyIndex += 1” is in wrong place, I suppose. Try to put it like it’s shown in the “Hints” menu (upper/right corner of display).

I checked your code and with right formatted indents and replacing enemyIndex it worked.

As for questions… I’m not professional in programming, and maybe someone could explain it better, but for me the answers would sound like “a - you create two counters (<> and <<killed + alive enemies>>) and compare them, and if amount of alive enemies > then dead foes, you command your hero to attack all alive enemies, except yaks. Over and over again.
And b - because of error warning <>. It often (not always, but most of time) causes your code and hero to stop”.
Good luck and happy coding)