請教 不知道哪裏錯了?就是不運行


#1


#2

I assume you are asking what’s wrong with your code…?
ok first thing:
the length of the array enemies is len(enemies), not enemies.length. Im not sure if enemies.length works, since i never tried it.
second thing:enemies = hero.findEnemies() should be in the loop since the array changes as more enemies appear.
the default code of the level is:

# 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)

    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
        
        pass

    # Between waves, move back to the center.
    

What you want to do is follow the tips i now add in the code:

# 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.**
    while condition:
    # Find the array's length with:  len(enemies) <-- this'll help you write the condition
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        **# While the enemy's health is greater than 0, attack it!** <-- inside the if statement condition loop
        while condition:
            hero.doSomething()
        pass

    **# Between waves, move back to the center.** <-- do this inside the while loop
    hero.moveXY(centerx, centery)

Follow those tips and you can complete the level easily.


#3

不是這樣enemies.length 但應該是 len(enemies).


#4

Think you . but still no working.


#5


but still not working,
what’s wrong with the code!


#6

Try doing this:
Instead of while enemyIndex < len(enemies):, do this:

while True:
    if enemyIndex < len(enemies):
        #if enemy is not yak:
            #attack attack attack
            enemyIndex += 1
    else:
        #move to center

#8


but not yet !!!:sweat_smile:


#9

Use

for enemy in enemies:
    #code here


#10

@Chaboi_3000 i don’t think you learn for in the desert…

you just have to put the if in the while loop, so basically highlight everything below line 6 and press tab @Law_VITET


#11


the hero was not go working .i don’t know how to let him gone!!!:rofl:


#12

Indent and also maybe you need more health?


#13

just delete the space on line 6…
COME ON! (not shouting @Chaboi_3000 :wink: ) you can do this… :slight_smile:


#14


It still doesn’t work !!!:grin:


#15


Please, what do u think?
It still so motionless:joy:


#16

Can you video tape it using quicktime player?


#17

@Law_VITET did you press shift + enter to run the code?
if that doesn’t work, then you need better gear.


#19

2017-10-12-1303-02_20171012130822


#20

2017-10-12-1303-02_20171012130822


#21
if enemy.health>0:
        hero.attack(enemy)

is wrong because of indentation
Use this instead:

if enemy.health>0:
    hero.attack(enemy)

#23

I tried the same.hero dose not move:grin: