259222
January 26, 2020, 8:14pm
1
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.
259222
January 26, 2020, 10:37pm
3
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.
259222
January 27, 2020, 12:16am
5
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
wazi
September 21, 2020, 10:29am
9
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)
wazi
September 22, 2020, 9:31am
10
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)
AnSeDra
September 22, 2020, 9:35am
11
Can you please try to format your code as it is described below so we will be able to help you solve the level?
Formatting your code before posting code is essential to our community, and if you don’t we might not be able to help Everyone must know how to format ALL of your code properly. It’s really easy and only requires a very small amount of effort.
[Screen Shot 2020-04-04 at 11.55.07]
To post your code from the game , use the </> button or it won’t format properly. When you click the </> button, the following will appear:
[Screen Shot 2020-04-04 at 11.55.36]
Please paste ALL …
Andrei
wazi
September 22, 2020, 9:52am
14
I figured it out, thank you!
1 Like
AnSeDra
September 22, 2020, 9:55am
15
Congratulations for completing the level!
Andrei
Mumbo_6
September 22, 2020, 9:56am
16
Congrats! Please put a tick on your post so that people know that they can find a solution here
1 Like
AnSeDra
September 22, 2020, 9:57am
17
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
system
Closed
September 22, 2020, 9:59pm
19
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.