# Backwoods Treasure - Help in Python

``````

loop:

enemies = self.findEnemies()
enemyIndex = 0

coins = self.findItems()
coinIndex = 0

self.moveXY(53, 18)

while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
enemyIndex += 1

if enemy:

while enemy.health > 0:
if self.isReady("power-up"):
self.powerUp()
elif self.isReady("bash"):
self.bash(enemy)
else:
self.attack(enemy)

while len(coins) > coinIndex:
coin = coins[coinIndex]
coinIndex += 1

if enemies:
break
if coin:
self.moveXY(coin.pos.x, coin.pos.y)

``````

Iâ€™ve been trying to make my hero stop grabbing coin and attack an enemy if there is one but he either attacks only enemies or grabs coins. Iâ€™ve tried using distanceto, if not enemy, and a few other attempts. Any ideas?

The code you have above does:

loop
â€“ get enemies and coins
â€“ kill all enemies until there are no more
â€“ (then) pick up all coins until there are no more
â€“ (never loops back to enemies since there are probably always coins)

make it:

loop:
â€“ get Nearest-enemy and Nearest-coin
â€“ if enemy attack
â€“else if coin pick up

then it will attack if there are enemies and get coins otherwise . . . one at a time

you could also make the first if a â€śif enemy and distance < #â€ť then it will pick up coins until they get closeâ€¦

1 Like

my code always says to switch the item x and y to my heroâ€™s x and y, how do i fix it?

We would have to see the code to understand the exact nature of the error?

item.pos.x,
item.x,
which command,
etcâ€¦