# Defeat shamans to survive.
# This function finds the weakest enemy.
def findWeakestEnemy():
enemies = hero.findEnemies()
weakest = None
leastHealth = 99999
enemyIndex = 0
# Loop through enemies:
if enemy:
# If an enemy's health is less than leastHealth:
if enemy.health < leastHealth:
# Make it the weakest
enemy = weakest
# and set leastHealth to its health.
leasethealth = enemy.health
return weakest
while True:
# Find the weakest enemy with the function:
weakestShaman = findWeakestEnemy()
# If the weakest enemy exists:
if weakestShaman:
# Attack it!
hero.attack(weakestShaman)
def findWeakestEnemy():
enemies = hero.findEnemies()
weakest = None
leastHealth = 99999
enemyIndex = 0
# Loop through enemies:
for enemy in enemies:
# If an enemy's health is less than leastHealth:
if enemy.health < leastHealth:
# Make it the weakest
enemy = weakest
# and set leastHealth to its health.
leastHealth = enemy.health
return weakest
while True:
# Find the weakest enemy with the function:
weakestShaman = findWeakestEnemy()
# If the weakest enemy exists:
if weakestShaman:
# Attack it!
hero.attack(weakestShaman)
Here’s the problem. You’re redefining enemy as weakest, which is None. So leastHealth = enemy.health is really leastHealth = None.health. To fix that, just switch enemy and weakest. So weakest = enemy.
Hmm … maybe try submitting a couple of times. You also might want to wait a couple of seconds first, it could be that not all of the shamans had spawned.
# Defeat shamans to survive.
# This function finds the weakest enemy.
def findWeakestEnemy():
enemies = hero.findEnemies()
weakest = None
leastHealth = 99999
enemyIndex = 0
# Loop through enemies:
for enemy in enemies:
# If an enemy's health is less than leastHealth:
if enemy and enemy.health < leastHealth:
# Make it the weakest
weakest = enemy
# and set leastHealth to its health.
leastHealth == enemy.health
return weakest
while True:
# Find the weakest enemy with the function:
weakestShaman = findWeakestEnemy()
# If the weakest enemy exists:
if weakestShaman:
# Attack it!
hero.attack(weakestShaman)
I tried replacing the loop with what @PeterPalov suggested, but it cause my code to completely break, as I did not have a defined enemy.