# [SOLVED] Python Blind Distance

hi i am stuck i need help @Chaboi_3000 @Chaboi @anonymous
def nearestEnemyDistance():
enemy = hero.findNearestEnemy()
result = 10 # you set the default distance to enemy to 10
if enemy: # and if there is no enemy
result = hero.distanceTo(enemy)
return result # the distance to enemy will be 10
# if there is no enemy
while True:
enemyDistance = nearestEnemyDistance()
if enemyDistance > 0: # this will be always true
# 10 or the real distance
hero.say(enemyDistance)

Why do you think the wizard tires out?..he’s casting on every iteration of the loop. There are times when you don’t want him to case, therefore saving energy.

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:
if enemyDistance > 0 :
# Say the value of enemyDistance variable.
hero.say(enemyDistance)
enemy = hero.findNearestEnemy()
if enemy:

can you please help me i cant move on if i cannot get past this level!!![quote=“Innerdesert4346, post:1, topic:11338, full:true”]
I’m trying to complete blind distance but i always run out of time because the wizard gets too tired too fast because he needlessly attacks when he doesn’t need too so i fail i have tried using stuff that isn’t involved in the level but no matter what it fails over and over its frustrating.
[/quote]

Zax, once again, it is your line 3 that is causing the problem…I see that you fixed the ‘hero.say’ statement I copied your original code and ran it…yes, the wizard got tired. I changed line 3, as I had recommended, and passed the level.

Also, you’ve also added a new if statement…you don’t need this; you don’t want your hero to attack.

thx
(20 characters)

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.

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

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!

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")
`

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

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