Sarven Treasure/Help /Stopping

Hey i need help to pass this one level and my character stops in the middle of the challenge

while True:
    items = hero.findItems()
    enemy = hero.findNearestEnemy()
    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:
            bestItem = item
            bestValue = item.value / hero.distanceTo(item)
        itemsIndex += 1
    
    if enemy:
        NEDistance = hero.distanceTo(enemy)
    if bestItem:
        BIDistance = hero.distanceTo(bestItem)
    
    if enemy and NEDistance <= 5:
        if hero.isReady("dash"):
            hero.dash(enemy)
        elif hero.isReady("cleave"):
            hero.cleave(enemy)
        elif hero.isReady("punch"):
            hero.punch(enemy)
        elif hero.isReady("bash"):
            hero.bash(enemy)
        else:
            hero.attack(enemy)
    elif bestItem and BIDistance >= 10 :
        if hero.isReady("dash"):
            hero.dash(bestItem)
        else:
            hero.moveXY(bestItem.pos.x, bestItem.pos.y)
    elif bestItem and BIDistance <= 9 :
        hero.moveXY(bestItem.pos.x, bestItem.pos.y)

Hi Some_Guy,

I’m thinking these lines might be holding things up. Sometimes stacked if statements don’t run quite as you expect.

You could take out the ‘if bestItem’ - you’ve just found it, so it should exist. You could then put the NEDistance definition inside the ‘if enemy and NEDistance <= 5’, as that will only run if there’s an enemy.

Keep trying different things!

Jenny

2 Likes

Like this?

while True:
    items = hero.findItems()
    enemy = hero.findNearestEnemy()
    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:
            bestItem = item
            bestValue = item.value / hero.distanceTo(item)
        itemsIndex += 1
    
    if enemy:
        NEDistance = hero.distanceTo(enemy)
    BIDistance = hero.distanceTo(bestItem)
    if enemy and NEDistance <= 5:
        if enemy:
            NEDistance = hero.distanceTo(enemy)
        if hero.isReady("dash"):
            hero.dash(enemy)
        elif hero.isReady("cleave"):
            hero.cleave(enemy)
        elif hero.isReady("punch"):
            hero.punch(enemy)
        elif hero.isReady("bash"):
            hero.bash(enemy)
        else:
            hero.attack(enemy)
    elif bestItem and BIDistance >= 10 :
        if hero.isReady("dash"):
            hero.dash(bestItem)
        else:
            hero.moveXY(bestItem.pos.x, bestItem.pos.y)
    elif bestItem and BIDistance <= 9 :
        hero.moveXY(bestItem.pos.x, bestItem.pos.y)