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
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:
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.