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”)
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.
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.
Hi @Student_Spencer_Bull, welcome to the CodeCombat Discourse.
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
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!
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! 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 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 !
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
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