# Metal Detector - Python

I got problem with this level. I’m not sure if I made mistake in code and hero saying distance to coin few times and thats why cannon shots once in front of hero then twice at coin and again twice at top coin after ogre collect it.

Each coin has a unique identifier
You can not just use id "Gold Coin"
Use findNearest() and findItems()

I know this one work but is it optimal solution for this exercise?

def coinDistance():
# Find the nearest coin,
coin = hero.findNearestItem()
# If there is a coin, return the distance to it.
if coin:
distance = hero.distanceTo(coin)
return distance
# Else, return 0 (zero).
else:
return 0
pass

while True:
distance = coinDistance()
if distance > 0:
# Say the distance.
hero.say(distance)
pass

Solution corresponds to that expected of you

This may cause an error
your current glasses do not have this skill

hero.findItems
hero.findNearest

or

hero.findByType( “coin” )
hero.findNearest

Thanks for help!

# Write the function.

def coinDistance():
# Find the nearest coin,
distance = hero.distanceTo(“Gold Coin”)
# If there is a coin, return the distance to it.
if “Gold Coin”:
hero.say(distance)
# Else, return 0 (zero).
else:
return 0
pass

while True:
distance = coinDistance()
if distance > 0:
# Say the `distance`.
hero.say(distance)
pass

Hi, please could you format your code so we can see the indents. Topic with instructions here: How to Post Your Code With Radiant, Harmonious Formatting.
Thanks
Danny

Hi everyone I use these commands and it works,

``````# The artillery uses coins as a target.
# You'll be the rangefinder for the artillery.

# Write the function.
def coinDistance():

Edited: I deleted the solution``````

Welcome welcome @yigit to the forum! It is a lovely place where you can share all kinds of stuff (appropriate of course), share bugs, and even get assistance for code! We suggest that you review this topic which shows all essentials of this board! And we suggest you review this topic which shows how to post your code correctly! Thanks!!

Please remove the solution as it is against the Discourse rules.

Lydia

3 Likes

I really need help. I’m really stuck. I use Java Script.
Here my code. plz find out what wrong and reply so i can fix it.
:
// The artillery uses coins as a target.
// You’ll be the rangefinder for the artillery.

// Write the function.
function coinDistance() {
// Find the nearest coin,
hero.distanceTo(“Gold Coin”);

``````// If there is a coin, return the distance to it.
if(item){
return hero.distanceTo(item);
// Else, return 0 (zero).
}else{
return 0;
``````

}
}
while (true) {
var distance = coinDistance();
if (distance > 0) {
// Say the `distance`.
hero.say(distance);
}
}

y u delet solution???

it is america, we do what we want.

Hi, could you post your code correctly as it says here? Thanks.

Yigit deleted the solution because posting solutions or requesting them is against the forum. This forum is a place to learn,not to “cheat” or “copy”.

Some people on this forum aren’t in America . . .
Even if they are in America, there are rules and laws you have to obey to be “free”.

The reason we tell people to post their code correctly is because in Python, if you indent the wrong thing, your code won’t work. So we need to see your code formatted correctly in order to help you.
Lydia

2 Likes

well I use JavaScript instead of Python sooo… yeah. srry

1 Like

Well, even if you use JavaScript, it is easier for us to figure out what is wrong with your code when you format it correctly. So can you please, format your code correctly?
Lydia

Yes, in fact it’s pretty much essential. All high level programming languages (python, javascript, coffeescript, ruby, java etc.) depend on indentation. Indentation is how much each line is pushed forward. For example after an if statement it is required that you push the next line forward 4 spaces, right? If we can’t see that or copy it from your code we have to try and work out the problem without it or try and put in the indentation ourselves. There are two problems with that: firstly that it’s a waste of our time when you could spend 30 seconds pressing a button and pasting in your code from the game, and secondly the problem in your code could actually be an indentation one, and we would never know because your code wouldn’t be formatted.

I’ve made it even easier to see, so just spend a few minutes reading it.
Thank you,
Danny

2 Likes

okay, thanks! I really don’t use this site much so I don’t know how to use it well.