Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Diamond dozen help (python)

My code isn’t working I don’t know what i’m doing wrong.

Here is my code:

# Claim the coins while defeating the marauding ogres.

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):
        if items[itemsIndex].value>bestValue:
            bestItem=items[itemsIndex]
            bestValue=bestItem.value
        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:
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
                hero.moveXY(coin.pos.x, coin.pos.y)

@259222, welcome to the forum!

I combined the valueOverDistance function with findBestItem and came up with findOptimalCoin. In your code, you are not calling the valueOverDistanceFunction.

Figure out where/when you should call the value function, or possibly, try combining them…the calculation performed in the function is certainly handy.

So I used your advice and this is my current code:

# Claim the coins while defeating the marauding ogres.

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):
        if items[itemsIndex].value>bestValue:
            bestItem=items[itemsIndex]
            bestValue=bestItem.value
        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:
            valueOverDistance(coin)
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
                hero.moveXY(coin.pos.x, coin.pos.y)

But it still doesn’t work. Did I call the function in the wrong place?

Yes…it should actually be a clause in the ‘if items’ statement, in the findBestItem function.

I can’t figure out a good way to explain what/where to change your code, without just providing a solution…as I mentioned, I used a completely different method. I can PM you my code for that function, if you wish though.

This is my current code:

# Claim the coins while defeating the marauding ogres.

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):
        if items[itemsIndex].value>bestValue:
            valueOverDistance(coin)
            bestItem=items[itemsIndex]
            bestValue=bestItem.value
        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:
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
                hero.moveXY(coin.pos.x, coin.pos.y)


On the return item.value / hero.distanceTo(item) it says that “cannot read property ‘value’ of null”

what does that mean?

P.S. you said

what does ‘PM’ mean?

It means ‘private message’…this way, we avoid sharing a potential spoiler with everyone.