# Diamond dozen-help needed-[Python]

Hello, I would need help with my code in level Diamond dozen:
In the beginning I couldn’t even start the code, the instructions were that unclear. After viewing others’ code, I came to this conclusion.
My problem is: I can’t get to the coins in time, I bought the “best” shoes, so I can jump to the coin, too. The problem is, that my hero won’t jump, only walk. The other thing is, do I really need over 1000 Health? Sometimes I die, sometimes I don’t, but I only survive with like, 5 Health?
I am not even sure if my main code is good, sometimes my hero only goes to the bronze coins…

``````# 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.
while itemsIndex<len(items):
item=items[itemsIndex]
if item.value>bestValue:
``````
``````            bestValue=valueOverDistance(item)
bestItem=item
# Find the item with the highest 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)
if coin:
hero.jumpTo(coin)
else:
hero.moveXY(coin.pos.x, coin.pos.y)

``````
``````
``````

I see a couple problems here. The big one being with this section of code:

``````while itemsIndex<len(items):
item=items[itemsIndex]
if item.value>bestValue:
bestValue=valueOverDistance(item)
bestItem=item
# Find the item with the highest valueOverDistance()
itemsIndex+=1
return bestItem
``````

I fixed the indentation on this code, because it’s really hard to tell if yours is correct due to the formatting of your post. Now, the problem is your if statement. What you’re asking, is if the value of one coin is greater than the value of another, then you get the valueOverDistance of the coin. This is backwards. What you need to do, is run your loop of items (item) through the valueOverDistance function. this will give you the true item value for each coin based on how far you have to travel to get it. Then you need to check if the true value of that particular coin is better than the current bestValue. If it is, then that value is now the best value, and that coin is now the best coin.

Another problem I see, based on what you posted, is your coin statement:

``````if coin:
hero.jumpTo(coin)
else:
hero.moveXY(coin.pos.x, coin.pos.y)
``````

As far as I can tell, coin is not defined anywhere in your code. You will need to make sure that coin is making use of the findBestItem() function when you define it. Also, you can take out the jump statement, your hero is only moving towards the coins you’re telling it to, once you fix the code you won’t have to jump anywhere. Hope this helps.

2 Likes

Thank you very much for answering! I fixed the first part like this (the formatting was good):

``````  while itemsIndex<len(items):
item=items[itemsIndex]
# Find the item with the highest valueOverDistance()
if valueOverDistance(item)>bestValue:
bestValue=valueOverDistance(item)
bestItem=item
itemsIndex+=1
return bestItem
``````

Now my hero perfectly finds the best coins!
My coin defining was magically cut out? I remember copying it 100%, but yes, I didn’t forget to define it:

``````    else:
items = hero.findItems()
coin = None
coin = findBestItem(items)
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
``````

Now I can collect about 30-37 coins, but the game still says that that is not enough.

It is really hard to tell what is going on, could you please cut and paste your whole program?

FYI: It isn’t about how many coins you get. I just tested that and I was able to collect 51 coins and still loose because I didn’t get the most valuable coin each time. I changed my gear to the starter sword which slowed me down a lot and I was able to win by only getting 30 coins as long as I got the best one each time.

I tried again later, and was able to pass the level. I guess It was luck?
And yes, it wasn’t the overall value (I got like 46 coins), just the most valuable ones.
Thanks for helping!