[SOLVED] 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.

Could someone please help me? This 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()
    valueOverDistance()
    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)

It highlights item value in this phrase, return item.value / hero.distanceTo(item) , and says "cannot read property ‘value’ of undefined

Compare how you write the function:

def valueOverDistance(item):

with how you’re calling it:

valueOverDistance()

What’s the difference?

Jenny

2 Likes

hello I need help in this
here is my code

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
    
    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)

hello I’m back and I’ve change my code here it is


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()
    
    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)

Can you please try to format your code as it is described below so we will be able to help you solve the level?

Andrei

I have formatted AnSeDra

I figured it out, thank you!

1 Like

Congratulations for completing the level!

Andrei

Congrats! :partying_face: Please put a tick on your post so that people know that they can find a solution here

1 Like

It is not @wazi’s topic, so he can not mark any post as the solution. But I edited the title to let the folks know that the topic is solved.

Andrei

Ok, thanks @AnSeDra!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.