[SOLVED] Python Blind Distance

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")
  1. You want the if statement to check that enemyDistance is greater than 0, not hero.distanceTo(enemy).
  2. You don’t need the line setting nearestenemyDistance at all.
  3. You want the hero to say the enemyDistance, not just say 0.

Try changing those, hopefully that helps things!

1 Like

help
please help me to code it .I am stuck.what I need to do for make it right?

2 Likes

Hi @Armin_Alimohammadi and welcome to the forum! :partying_face:

Can you format your code as it is described below so we will be able to help you at this level?

2 Likes
`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.

2 Likes

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

1 Like

I deleted (" " mark) but noting happend

1 Like

Can you give me a direct link to the level?

1 Like

Here what the comment tells you to make sure that it is greater than 0?

Andrei

1 Like

https://codecombat.com/play/level/blind-distance?

what is problem at that code?

Your repeating this line again even thought theres this line above try deleting this.

1 Like

And do not forget to also check this

Andrei

2 Likes

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

1 Like

    # 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

1 Like

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