``````def findMostHealth(enemies):
target = None
targetHealth = 0
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.health > targetHealth:
target = enemy
targetHealth = enemy.health
enemyIndex += 1
return target

def valueOverDistance(item):
return item.value / hero.distanceTo(item)

# Return the item with the highest valueOverDistance(item)
def findBestItem(items):
bestItem = None
bestValue = 0
itemsIndex = 0

# Loop over the items array.
# Find the item with the highest valueOverDistance()
while itemsIndex < len(items):
item = items[itemsIndex]
if item.value / hero.distanceTo(item) > bestValue:
bestValue = item
bestItem = valueOverDistance(item)
itemsIndex += 1
return bestItem

while True:
enemies = hero.findEnemies()
enemy = findMostHealth(enemies)
if enemy and enemy.health > 15:
while enemy.health > 0:
hero.attack(enemy)
else:
coins = hero.findItems()
coin = None
coin = findBestItem(coins)
if coin:
hero.moveXY(findBestItem.pos.x,findBestItem.pos.y)

``````

I donât know why but it says my âxâ in âfindBestItem.pos.xâ is undefined. Thoughts?

Howdy and welcome to the forum!

I see two problems:

1. Lines 28 and 29âŚyou have your variables transposed. bestValue should call the valueOverDistance function; bestItem should equal item
2. Your moveXY statement is not referencing coin.

I corrected these and the code runs properly.

1 Like
``````def findMostHealth(enemies):
target = None
targetHealth = 0
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.health > targetHealth:
target = enemy
targetHealth = enemy.health
enemyIndex += 1
return target

def valueOverDistance(item):
return item.value / hero.distanceTo(item)

# Return the item with the highest valueOverDistance(item)
def findBestItem(items):
bestItem = None
bestValue = 0
itemsIndex = 0

# Loop over the items array.
# Find the item with the highest valueOverDistance()
while itemsIndex < len(items):
item = items[itemsIndex]
if item.value / hero.distanceTo(item) > bestValue:
bestValue = valueOverDistance
bestItem = item
itemsIndex += 1
return bestItem

while True:
enemies = hero.findEnemies()
enemy = findMostHealth(enemies)
if enemy and enemy.health > 15:
while enemy.health > 0:
hero.attack(enemy)
else:
coins = hero.findItems()
coin = None
coin = findBestItem(coins)
if coin:
hero.moveXY(coin.pos.x,coin.pos.y)
``````

So I think I fixed it, but now my hero just gets the nearest coin.

You have definitely fixed the moveXY issue, but not quite there on line 28. You should be calling a function on this line and passing item as a variable.

``````def findBestItem(items):
bestItem = None
bestValue = 0
itemsIndex = 0

# Loop over the items array.
# Find the item with the highest valueOverDistance()
while itemsIndex < len(items):
item = items[itemsIndex]
if valueOverDistance > bestValue:
bestValue = valueOverDistance
bestItem = item
itemsIndex += 1
return bestItem
``````

Sorry, but I donât understand what you mean by âpassing item as a variableâ

This is from your first code posting:

``````            bestItem = valueOverDistance(item)
``````

As discussed, bestItem is not the correct variable, so you fixed it with:

``````            bestValue = valueOverDistance
``````

See the difference? valueOverDistance is the name of a function, which is expecting some object (vairable) to be passed to it when called:

``````def valueOverDistance(item):
return item.value / hero.distanceTo(item)
``````

item is the object being passedâŚwhen you âcallâ valueOverDistance, you do so by assigning the function call to a new variable name, such as bestValue, however, since valueOverDistance is expecting to be given (passed) something, you need to do so. Something like:

``````bestValue = valueOverDistance(item)
``````

OK, that makes more sense thank you so much!

1 Like