Need help with Diamond Dozen (Python)

I have two problems. One: I can’t get the warrior to go after the coins. Two: I keep running out of hearts.
Here’s 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)
    flag = hero.findFlag()
    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)
    if hero.health < 750:
        hero.consecrate()

I don’t understand what’s wrong with it

Hi, @CodeCombatWarrior! Welcome to the Discourse :partying_face:
Can you show us a screenshot of your equipment(armor, sword, etc.)? Thanks :smiley:
Link to Level(for people who might want it): CodeCombat - Coding games to learn Python and JavaScript

Rachel

1 Like

There is already a topic for this here.Diamond dozen(python) - #16 by lukas31

@lukas31, unlike for off-topic topics, you are allowed to make a new topic to get help with a level. In fact, it’s actually quite helpful, because it means we can solve the topic and shut it after it’s finished.
@CodeCombatWarrior, the problem here is that you haven’t written this function:

You’re supposed to be finding the bestItem. Right now, you are returning None (that’s what you defined bestItem as).
You have to use a similar process to the findMostHealth(enemies) function, but for coins. In fact, it’s almost exactly the same, you just need to use valueOverDistance(item) instead of .health and item and items instead of enemy and enemies.
Try and loop through the items and find the best one, then return it.
If that doesn’t work, post your code and I’ll look at it.
Danny

It worked, thanks.

2 Likes

Please mark whichever post helped solve your problem.

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