Sarven Shepard Need help


#1

Can someone please help me!
Please note that if I start the enemyIndex = 0 as provided by the pre-written code the hero never attacks any enemies. When I change the enemyIndex = 1 the hero attacks the first wave of enemies and then returns to center, but when the next wave of yaks and ogres come the hero never seems to find the next wave of ogres (i.e. enemies). When I watch the game run you can see the hero is not finding the enemy for the second wave. First of all in theory the code should work even if enemyIndex = 0. In fact in python list always start with position zero not one. Since the hero is not finding enemies after he returns that implies that once the hero moves back to center the program ceases at that point, but since the else statement is nested within the while True loop I would think it would loop back up to the top restart from the while True: portion of the code, but clearly that is not happening. Any help, suggestions, corrections would be greatly appreciated. This has been driving me crazy! lol

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

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.
    else:
        hero.moveXY(40,32)

#2

For some reason when I past my code into window it separates the first three lines of code and makes it look like the while enemyIndex < len(enemies) is not indented or nested within the while true loop. In the original code it is indented.
Thanks


#3

How should you get of the while enemyIndex < len(enemies): loop when the enemy.type is “sand-yak”?


#4

byobcello
The code is not if enemy.type is “sand-yak”. If you look above the code is if enemy-type != “sand-yak”:
!= means not equal to. Therefore the if statement states that if enemyIndex < len(enemy) total amount of enemies found and the enemy.type is not equal to “sand-yak” then start the nested loop below the if statement.
I hope that makes sense. Thank you.


#5

The else: can also be understood as elif enemy.type == "sand-yak":


#6

Sorry i am not exactly sure what your are suggesting I change… Are you saying the hero.move(x,y) statement not be placed under the else; statement? If so what do you suggest.
in the hint section it states the move(xy) command should be after(outside) your while enemyIndex loop but inside the main while True loop.


#7
    if enemy.type != "sand-yak":
        while enemy.health > 0:
            hero.attack(enemy)
        enemyIndex += 1
    else:  # this line is like: elif enemy.type == "sand-yak"
        hero.moveXY(40,32)

Work your way through the while loop starting with an enemy that has a type of "sand-yak".
(Also, don’t forget to change enemyIndex back to 0 before the while enemyIndex < len(enemies): loop)


#8

I thought thats what I wanted it do. Another words if the found that the enemies are only “sand yaks” then move back to the center. I definitely understand that my code is not resetting the enemyIndex back to zero, but I am not sure how to do that because I have to add to the enemyIndex in the loop so that it iterates through loop of all enemies in the array that appear on the screen. . the other problem is when I initially set enemyIndex = 0 the hero never attacks the first wave ogres which I don’t understand. Yet if I set it to 1 the hero at least attacks the first wave of enemies.


#9

Have you played this level and figured out a solution?


#10

So, let’s start with an enemyIndex of 0.
Assume the first element of your enemies list is actually an enemy of type "sand-yak".

while enemyIndex < len(enemies)

enemyIndex is 0, and let’s say the length of enemies is 3.
So, enemyIndex < len(enemies), and the while loop is entered.

enemy = enemies[enemyIndex]

The first element of enemies (which is a sand-yak) is now stored in enemy.

if enemy.type != "sand-yak"

The enemy.type is sand-yak, so the statement enemy.type != "sand-yak" evaluates to False.
Skip to the else.

else: # elif enemy.type == "sand-yak":
    hero.moveXY(40,32)

All we do now is just have the hero move.
The code inside the while loop ends here, so go back to the start of the while loop and run through it a second time.

Click here if you need a hint

Pay special attention to the enemyIndex. When does it change? When should it change?