How to pass Blind Distance

I don’t know how to pass this level. Can anyone help?
Here is my code :

Tell the wizard the distance to the coming ogres.

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.
distance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
if distance > 0 :
# Say the value of enemyDistance variable.
hero.say(“distance”)

I think this is complicated :slight_smile:

1 Like

You have defined the variable distance. Then when you call the variable with the hero.say method, you call it as a string. Once defined, a variable is not a string.

1 Like

I don’t know what is a string XD
Can you explain?

String - a type of programming data that represents text.

Variable - a symbol that represents data.

In your case, you have defined the variable distance as the function, nearestEnemyDistance().

distance = nearestEnemyDistance()

A variable is not just a string of text, it is a value of data. When you use the method, hero.say(), you call the variable as a string, which, to the program, is just a bunch of letters with no meaning and no value. It is a string because it is in quotes. No quotes - it’s a variable. With quotes - it’s a string.


1 Like

Thank you for sending me this. I worked on it, but the thing is, the blind magician just gets tired out. What should I do?

I just figured it out!
THANK YOU SO MUCH!

Mod edit: Please refrain from posting solutions

Hi @Student_Spencer_Bull, welcome to the CodeCombat Discourse. :tada:
Nice code! I like your use of a function. But if it’s alright please could you remove the complete solution, as it’s against the purpose of the forum. Here we just try and help people with their code rather than post solutions.
Thanks
Danny

oh thank you I was stuck on this level for like forever thankyouthankyouthankyou!!!

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!

hello i need some help

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.
distance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
if distance > 0 :
# Say the value of enemyDistance variable.
hero.say(“distance”)

First of all, you need to indent your code, otherwise parts of your code will not run correctly. Please check that all of your code is correctly indented. Second, in the last line, you are calling hero.say(“distance”), but that is making your hero say the string-form of distance, not the variable. Remove the quotes and it should work.

Hi again, i did what you said but it didnt work! :frowning: It seems to be the return result.

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

I did the indent too its just not working!

Replace hero.say(Distance) with hero.say(enemyDistance)

i cant do it. its keep saying: There is no enemy. Use enemy = hero.findNearestEnemy() first. pls help. this is my code:

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.
   distance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
if distance > 0 :
# Say the value of enemyDistance variable.
   hero.say(enemyDistance)

Hi @Ashton_Gifford, welcome to the forum :tada:!
The problem here is that you’ve used two variables for what should be the same variable. The comments tell you to create a variable called enemyDistance here:

However, you called yours distance:

That would be fine, as long as you are consistent. Here, you used enemyDistance, which you haven’t defined!

You don’t need to redefine enemyDistance as distance necessarily, but what you do have to do is make sure both of those variables (distance and enemyDistance) have the same name. They’re supposed to be exactly the same thing.
I hope this helps.
Danny

@Deadpool198 its still telling me the thing about the enemy variable. what do i do about that?

Please could you post your new code. I may not have told you what I meant properly, but I did test it with my change and it did work, so you may not have made the right change.
Danny

here’s my code:

`# Tell the wizard the distance to the coming ogres.

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:

if enemydistance > 0 :

Say the value of enemyDistance variable.

hero.say(enemydistance)
`

i changed it to say enemydistance instead of distance. is that what you told me to do?