Metal Detector - Python


#1

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.


#2

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


#3

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


#4

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


#5

Thanks for help! :slight_smile: