[SOLVED]Mad Maxer - Python

My hero dies and I need help. There are no errors, though, but my hero doesn’t attack? Its weird. Heres my code and a picture of my screen

# Attack the enemy that's farthest away first.

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

gonna try to indent everything into the while true now.

Ok my hero attacks but dies.

Your code is right if you put them in the right places.

1 Like

You begin by checking for farthest, but then look for enemy. Keep in mind you need to stay with the same variable throughout, switching to a new variable that isn’t defined like enemy in this section will definitely prevent your code from running properly. Use the farthest variable you already have created instead of the enemy variable you are currently using in this section.

@AlwaysConfused what glasses did you use?

he used enchanted lenses

When i delete the nearest enemy i get an error saying there is no enemy.

Not exactly sure what you mean. My point was that you need to use the same variable that you defined and not create a new one.

if farthest:
    # Take out the farthest enemy!
    # Keep attacking the enemy while its health is greater than 0.
    while enemy.health > 0:  # enemy variable has not been defined, change to farthest that has been identified
        hero.attack(enemy) # same here, attack farthest

You can’t check for enemy.health if you haven’t declared an variable called enemy with a value. You have created a variable called farthest and should check the health on that and attack it.

2 Likes

So farthest is your enemy and you should use farthest to attack hero.attack(farthest)

why are you using while enemy when its if farthest?

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.