This function finds the nearest enemy and returns the distance to it.
If there is no enemy, the function returns 0.
def nearestEnemyDistance():
enemy = hero.findNearestEnemy()
result = 0
if enemy:
result = hero.distanceTo(enemy)
return result
while True:
# Call nearestEnemyDistance() and
# save the result in the variable enemyDistance.
enemyDistance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
hero.say(“10”)
# Say the value of enemyDistance variable.
Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
It is important that we understand how your code is indented in Python to capture any mistakes related to that. Please read through the linked topic to learn how to do that.
I believe the issue is following the one line checking the distance. You are missing this part of code to make sure the distance is greater than 0. # If the enemyDistance is greater than 0:
Also, make sure you say the variable, and not 10 like you currently have coded.
def nearestEnemyDistance():
enemy = hero.findNearestEnemy()
result = 10
if enemy:
result = hero.distanceTo(enemy)
return result
while True:
# Call nearestEnemyDistance() and save the result in the variable enemyDistance
enemyDistance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
def nearestEnemyDistance():
enemy = hero.findNearestEnemy()
result = 10
if enemy:
result = hero.distanceTo(enemy)
return result