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
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.
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.