Sarven Shepherd help (Python)


#1

Whenever I run the code my hero just stands there. Please help me.

# Use while loops to pick out the ogre

loop:
    enemies = self.findEnemies()
    enemyIndex = 0

    # Wrap this logic in a while loop to attack all enemies.
    while enemyIndex < 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:
                self.attack(enemy)
                enemyIndex += 1
        pass

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

#2

Imagine what happens when the enemy at index 0 is a sand-yak. Think trough your program before looking into the spoiler.

Spoiler

Your program looks like this:

  1. Find enemies (let’s say this returns 6 entries, with the first entry being a Sand-Yak)
  2. Set enemyIndex to 0
  3. As long as 0 is smaller than … enemies is not a number?!?
    Use len(enemies) instead.
  4. (Given the error on line 4 didn’t happen)
    Get enemies[enemyIndex] (which is a Sand-Yak)
  5. If enemy.type ( == Sand-Yak ) equals sand-yak --> false, jump over if-block
  6. pass -> does nothing
  7. move to (40, 32)
  8. End of while, goto command 3.

As you can see (especially from line 8) you can see that you will never do anything else than moving to (40, 32), as enemyIndex never changes.


#3

Thanks a ton. I’ve been having trouble with while loops and now I see my error. Yet I still can’t get it right. when I run my updated code my hero kills two ogres then moves back to 40, 32. Then the “While enemyIndex < len(enemies)” I get a hard execution limit error. What am I doing wrong?

Use while loops to pick out the ogre

loop:
enemies = self.findEnemies()
enemyIndex = 1

Wrap this logic in a while loop to attack all 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!
        self.attack(enemy)
        enemyIndex += 1
    

# Between waves, move back to the center.

self.moveXY(40,32)

#4

Try incrementing enemyIndex regardless of whether the enemy is a yak or not.


#5

I breezed through the first initial 300 levels, it was simple enough, but on this level a slew of new definitions and functions come about and I am completely lost, been stuck for three weeks I really need help, I’ve tried absolutely everything.


#6

The “while True” function overrides everything as long as your main value is true, so everything else has to be within it. That was the problem I had, at least.


#7

Just write this :

Edited:
<snip> we don’t get correct code


#8

help, this is my code:
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)
enemyIndex += 1
pass

# Between waves, move back to the center.
hero.moveXY(40, 32)
it says that i need a : after  while enemy health > 0:

#9

can you post you code between the dots using </>
It is very hard to see clear without that since there is a lot of incrementation in this level