type or paste code here
```# Tell the wizard the distance to the coming ogres.
hero.say("0")
# 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:
enemy = hero.findNearestEnemy()
if enemy:
if hero.distanceTo(enemy) > 0:
# Say the value of enemyDistance variable.
nearestenemyDistance = hero.findNearestEnemy()
hero.say("0")
I did everything to make this work. But it says that the code never finished.
Make sure you follow what the comments say:
And:
Danny
It says that the result is zero, and then it says that the distance to the enemy is zero. So, I programmed my hero to say zero. But then it stopped at hero.findNearestEnemy.
These lines aren’t quite right:
if hero.distanceTo(enemy) > 0:
# Say the value of enemyDistance variable.
nearestenemyDistance = hero.findNearestEnemy()
hero.say("0")
- You want the if statement to check that enemyDistance is greater than 0, not hero.distanceTo(enemy).
- You don’t need the line setting nearestenemyDistance at all.
- You want the hero to say the enemyDistance, not just say 0.
Try changing those, hopefully that helps things!
please help me to code it .I am stuck.what I need to do for make it right?
Hi @Armin_Alimohammadi and welcome to the forum!
Can you format your code as it is described below so we will be able to help you at this level?
`while True:
# Call nearestEnemyDistance() and
# save the result in the variable enemyDistance.
enemyDistance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
if hero.distanceTo > 0:
enemyDistance = nearestEnemyDistance()
# Say the value of enemyDistance variable.
hero.say("enemyDistance")
`
please correct my code.
You have to say the the value of enemyDistance variable, thus you can delete the the quotation mark, because your saying “enemyDistance” instead of the variable itself.
Hope it helps
I deleted (" " mark) but noting happend
Can you give me a direct link to the level?
Here what the comment tells you to make sure that it is greater than 0?
Andrei
what is problem at that code?
Your repeating this line again even thought theres this line above try deleting this.
And do not forget to also check this
Andrei
And that is not why the code does not work. The code will work with the two of them if @Armin_Alimohammadi checks and fixed what I mentioned above. But it is a good observation and @Armin_Alimohammadi should also look to fix that. (Only the second one should be removed)
Andrei
# Call nearestEnemyDistance() and
# save the result in the variable enemyDistance.
enemyDistance = nearestEnemyDistance()
what is save the result what code i need to make?
Here put instead of hero.distanceTo the variabile enemyDistance.
And delete this line, not the other.
Andrei
what do i have to change in line 3 i tried changing it but it did not do anything
Hi hi12345678,
Welcome to the forum!
Can you post your code, and we can have a look at what you’re doing? There’s instructions on how to format it here.
Jenny