That code gets success in my level. Please could you post a screenshot of the level screen, and of your equipment. And also, could you post your code with indents: the gaps at the start of each line.
Put ``` on two new lines in the text editor of the forum, then post in your code directly from codecombat and it will have the spaces.
Danny
here’s my gear:
hear’s 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.
enemydistance = nearestEnemyDistance()
# If the enemyDistance is greater than 0:
if enemydistance > 0:
# Say the value of enemyDistance variable.
hero.say(enemydistance)
It still keeps saying this though.
Your indentation is still off. See how the lines aren’t in the right place on your post? Please put spaces at the start of the lines so I can see exactly how much indentation you have at the beginning of each line. You could also just post a screenshot of the actual level screen with the code and the map of the level.
Ah, I see. Indentation is the problem. If you have a function (starts with def) or a loop (like while True) all the rest of the code should be “inside” it. You got that right for the first line:
def nearestEnemyDistance():
enemy = hero.findNearestEnemy()
But the rest of it is too far to the left. It needs to go 4 spaces to the right—>
result = 0
if enemy:
result = hero.distanceTo(enemy)
return result
Same for the next bit. The first line is right:
while True:
# Call nearestEnemyDistance() and
# save the result in the variable enemyDistance.
enemydistance = nearestEnemyDistance()
But the rest needs to go 4 spaces right:
ok thanks. I’ll try it
This is my code and not working (see after return result) and when hero call distance (and say this)?
I need help and thank you.
Screen shot (Python code) in level map in English:
Mayde line 13 is if enemy: distance = …
line 14: say distance . I think is ok code? (only distance not " " this marks).
The only problem in your code @QED is this line:
if enemy.distance > 0:
enemy.distance is not a thing. enemyDistance, the variable you have just defined is a thing and is different from enemy.distance which won’t return any result.
So switch that to enemyDistance, because you need to use the variable you created in your function nearestEnemyDistance().
Danny
If no comments in code.
Thank you, I’m just testing in my code (in add while-true loop in after the line 12):
lines 13, 14, 15
But is not work yet.
This is this code:
and enemy Distance()
I just add this code after def (loop, after return result in while true loop:
I’m afraid that’s not quite right either.
You still need to check if enemyDistance > 0 and hero.say(enemyDistance). You don’t want to do:
if enemy:
You want:
if enemyDistance > 0:
Then you need to say enemyDistance, the variable itself, Not hero.distanceTo(enemy).
Danny
what am i doing wrong?
its not saying this anymore thankfully
but its still not… working
You’ve got the code itself exactly right. The only problem is the indentation. All of the lines in the nearestEnemyDistance() function need to be four spaces forwards, the same goes for the while True loop. So
enemy = hero.findNearestEnemy()
if enemy:
result = hero.distanceTo(enemy)
and:
if enemyDistance > 0:
hero.say(enemyDistance)
Both need to go forwards 4 spaces. Then it should work
You haven’t moved these lines four spaces forwards like I said:
That’s in the nearestEnemy function.
thank you. its working now.