Mad Maxer Help!


#1

Can anyone me with this

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = hero.findEnemies()
    
    # Look at all the enemies to figure out which one is farthest away.
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1
    
    # Is this enemy farther than the farthest we've seen so far?
    distance = hero.distanceTo(target)
    if distance > maxDistance:
        maxDistance = distance
        farthest = target
    
    if farthest:
        # Take out the farthest enemy!
        # Keep attacking the enemy while its health is greater than 0.
        while enemy.health > 0:
            hero.attack(enemy)
        pass

My hero doesn’t do anything


#2

You have 2 errors.
You only iterate trough enemies array without defining the farthest within the loop. Indent your code properly.
The enemy variable is void. You never initialized it.

But the level can be passed even without correcting the first error and I don’t know why ?!
Without correcting the indentation farthest = enemies [ len(enemies) - 1]
so the code can be written as:

while True:
    enemies = hero.findEnemies()
    enemy = enemies[len(enemies)-1]
    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)

And this has passed several times. Does it mean that in the enemies array from enemies = hero.findEnemies() the last element is always the farthest?
/PS: I will delete code if needed/


#3

thank you for your help


#4

Please could you not put topics where you need help with your code in the adventurer category which is for level making. uncategorized is the right category for that.
Thanks. :grin: