Diamond dozen(python)

This isn’t quite right. Please help!

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 valueOverDistance() > bestValue:
            bestValue = valueOverDistance()
        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)

Umm… valueOverDistance() needs an input. You didn’t specify an item for valueOverDistance().

I’m sry, I didn’t quite understand what to do from that. btw I haven’t seen you on before. Are you new?

well, im astounded a regular needs help.
also i dont know

1 Like

Well, I’m at the end of Mountain. Regular is just for discourse, not coco itself. But I’m not very good at coco to be honest.

i cant help you, im also in need of help, or im just to lazy to go back to desert

1 Like

You don’t need this

For this one, I first defined bestItem as -1000, then I created a variable “best” for the best coin and set that to None. Then I used a for-loop, (I forgot how I did it before and I came back to this after I’ve learned the for-loop) to run coin in coins. Then I used an if statement the checks if the coin.value / hero.distanceTo(coin) is greater than optimal. If it is, then optimal equals coin.value / hero.distanceTo(coin) and the next line, best = coin.
And the rest of your code looks good!
Lydia

1 Like

Thanks, that’s part of my problem going back from for loops.

Hmm… you could revisit some of the levels that introduces for-loops
Lydia

No, going back from for loops.

I don’t quite understand
Lydia

In the desert, it’s a little more complicated because you don’t have for loops. So after learning for loops, I’m having a hard time going back.

Your starting code is pretty much right as it is.
@Hin_Lee is correct:

What he means is that in your function you have a variable item in the brackets:

Which you use inside function:

This means that when you call the function in the findBestItem function, like you do here:

You need to put something in the brackets. You can’t have something in the brackets when you define a function, and not put it in when you call the function. You should put the item inside the brackets.
Finally you also need to update bestItem here:

The bestValue is the items valueOverDistance(item), then you need to define bestItem as the item otherwise item will be None when you return it here:

Danny

1 Like

Thanks, @Deadpool198! But I think I missed something you said, cuz I’m getting an error.


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 valueOverDistance(item) > bestValue:
            bestValue = valueOverDistance(item)
        itemsIndex += 1
        bestItem = bestValue
    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)

This bit is perfect.
This is what needs changing and moving to a different place:

bestItem should store the item with the highest value, not the value itself. You should put it inside the if valueOverDistance(item) > bestValue: statement because if the value isn’t greater than bestValue, then it’s not the most valuable coin. Now you have to define bestItem as you’re current item, not the current value. You’ve found the valueOverDistance(item) with the item, now you have to make it the bestItem.
Danny

You don’t need the value over distance function. I don’t have it, and my code works.
Have the find optimal coin function like this:
mod edit: please don’t post solutions.

1 Like