# Help with Sarven Shepherd (python) Solved

while True:
enemies = hero.findEnemies()
enemyIndex = 1
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
hero.attack(enemy)
if enemy.health > 0:
hero.attack(enemy)
else:
enemyIndex += 1
pass
hero.moveXY(40, 32)

I can’t see the format of your code because you didn’t post it properly but there are some discernible errors.

First is the assignment of enemyIndex = 1. enemyIndex should start at 0 and then increment by 1.
Second, the first hero.attack should be after checking enemy.health and the second hero.attack isn’t necessary.

You will have to post it correctly in order for anyone to see the structure. Please post correctly from now on. It’s really easy and helps us help you. Thanks.

Maybe this helps out:

``````while True:
enemies = hero.findEnemies()
enemyIndex = 1
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != “sand-yak”:
hero.attack(enemy)
if enemy.health > 0:
hero.attack(enemy)
else:
enemyIndex += 1
pass
hero.moveXY(40, 32)
``````

That’s better. Now we can see your code and help out

Well, you know how to do it now. But you copied it from here, not the game. It has to be copied from the game in order to have the formatting. When copying it from this board it has no formatting and there’s no indentations. You’re getting closer.

Also, in the previous post I discussed starting your enemyIndex at 0, not 1. You are starting your index at the second object in the array. Python starts counting at 0, not 1.

Three objects in an array. They are numbered, 0, 1, 2.

The first hero.attack should be after checking enemy.health and the second hero.attack isn’t necessary.

The else statement should be deleted.

when I put it at zero it will not attack anything what should I do

You have to post your code - copied from the game to here - in order for us to see your structure. If you’ve made the changes detailed above and the structure is correct, it will work. When you click on the button indicated above, it will tell you where to paste your code. It’s really easy.

``````while True:
enemies = hero.findEnemies()
enemyIndex = 1
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != "sand-yak":
hero.attack(enemy)
if enemy.health > 0:
hero.attack(enemy)
else:
enemyIndex += 1
pass
hero.moveXY
``````

Thank you for posting your code correctly. However, you didn’t make any of the three changes that I told you need to be fixed.

there is still an infinite loop

``````while True:
enemies = hero.findEnemies()
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != "sand-yak":
if enemy.health > 0:
hero.attack(enemy)
enemyIndex += 1
pass
hero.moveXY(40, 32)

``````

The line to increment enemyIndex += 1 is tabbed over too far. It should line up under the if enemy.type conditional statement.

thank you for helping me with the infinite loop problem

Can someone help me

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

# Wrap this logic in a while loop to attack all enemies.
# Find the array's length with:  len(enemies)
while True:
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:
hero.attack(enemy)
enemyIndex += 1
pass

# Between waves, move back to the center.
hero.moveXY(40, 33)
``````

hero won’t move

never mind figured it out

``````# 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 enemy == 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:
hero.attack(enemy)
pass

# Between waves, move back to the center.
else:
hero.moveXY(40, 32)

``````

My hero has a error it said property of ‘type’ is not defined

You need to check whether there is an enemy before you check it’s type.
-Danny

I’m having a problem with this level as well. My character tries to fight off the enemies, but just dies. Fails to “humans must survive.”

``````# 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 enemy.health > 0:
hero.attack(enemy)
enemyIndex += 1
# Between waves, move back to the center.
hero.moveXY(40, 32)
``````

Any assitance you can provide would be appreciated! Thanks.

Hi, welcome to the CodeCombat discourse.
Your code works fine for me, so it must be an equipment problem. If possible try and find the best armour/weapons for this level or play other levels to get gems to get better equipment.
If neither the first not the second option works, could you post a screenshot of your best equipment (don’t forget about rings, special abilities etc.) and another of the level screen.
Thanks,
-Danny

1 Like

You should maybe get better armor and better sword.

Hi there, my code isn’t working. I came up with this. Here’s my code. It keeps on getting stuck in an 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:
hero.attack(enemy)
pass

# Between waves, move back to the center.
hero.moveXY(40, 32)``````