#1

I don’t understand why my hero pick up the coin with the lowest value/distance rating. Any help?

``````loop:
bestCoin = None
maxRating = 0
coinIndex = 0
coins = self.findItems()

while coinIndex < len(coins):
coin = coins[coinIndex]
distance = self.distanceTo(coin)
value = coin.value
coinIndex = coinIndex + 1
if value/distance > maxRating:
maxRating = value
bestCoin = coin
if bestCoin:
self.moveXY(bestCoin.pos.x, bestCoin.pos.y)``````

#2

Try changing the middle line to:

``````maxRating = value/distance
``````

#3

It works perfectly. Thanks!!!

#4

I’m having the same trouble on this level. Based upon what I’ve read here, the code should be working.

``````# Collect more coins than your doppelganger.
# You only have a few seconds to collect coins. Choose your path wisely!
loop:
bestCoin = None
maxRating = 0
coinIndex = 0
coins = self.findItems()
# Try calculating "value / distance" to decide which coins to get.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
if coin.value / self.distanceTo(coin) > maxRating:
maxRating = coin.value / self.distanceTo(coin)
bestCoin = coin
if self.isPathClear(self.pos, bestCoin.pos):
self.moveXY(bestCoin.pos.x, bestCoin.pos.y)
``````

I’ve tried changing the position of coin.value and self.distanceTo(coin) but it doesn’t change the outcome. My guy appears to be heading for the lowest value coin. Sometimes he will head for the silver coins but I have yet to see him head for the gold first. Not sure what to do at this point.

Any help is greatly appreciated.

#5

If you are moving to `bestCoin` while you are still in the while-loop, you will be moving to the least value coin that still has a greater ratio than 0, that is, the worst coins possible. Move the part where you move to the coin outside the while-loop. That should fix your problem.

#6

Thank you! That worked perfectly.