Hi again, I’m having trouble with this level and I checked other posts but am still having the same issue.
Hero is not moving.
Here is my code:
while True:
closestGold = None
minGoldDist = 9001
coinIndex = 0
coins = hero.findItems()
if coinIndex < len(coins):
coin = coins[coinIndex]
if coin:
distance = hero.distanceTo(coin)
if coin.value == 3 and distance < minGoldDist:
closestGold = coin
minGoldDist = distance
if closestGold:
hero.moveXY(closestGold.pos.x, closestGold.pos.y)
coinIndex += 1
Your indentation is a bit off. Your final ‘if’ statement needs to be moved to the left a notch. Your coinIndex += 1 statement is currently a member of that same ‘if’ block…it needs to be the final statement of the ‘if coinIndex’ block instead.
ahh. I did think it looked odd. gotcha thanks,
I’ve changed it to this:
while True:
closestGold = None
minGoldDist = 9001
coinIndex = 0
coins = hero.findItems()
if coinIndex < len(coins):
coin = coins[coinIndex]
if coin:
distance = hero.distanceTo(coin)
if coin.value == 3 and distance < minGoldDist:
closestGold = coin
minGoldDist = distance
if closestGold:
hero.moveXY(closestGold.pos.x, closestGold.pos.y)
coinIndex += 1
but hero is still not moving, checked for typos and don’t see anything… any idea on what else could be wrong?
Wow…totally missed this one. On line 6, rather than using ‘if’ use ‘while’.
I ran your code this go around and discovered I was not exactly correct on alignments…your final ‘if’ block needs to be in line with the first ‘if’. This makes it a member (child) of the ‘while True’, so it runs only after all other loops have completed.
Your ‘coinIndex += 1’ statement needs to be the final statement of the ‘if coin’ block, so this means move it up to line 13.
2 Likes
worked great, thanks again
1 Like