Max Maxer Sells Out passed me even though code failed

Mad Maxer Sells Out awarded me with experience and gold even though the code failed an I collected a total of 30 coins instead of the 29 you collect with correct code (I suspect that it’s not 28 because the character stops on a coin spot and a bronze coin respawns under your feet or in your path at some point.)

Below is the bad code that passed. My hero is using the reenforced boots so he has a speed bonus.

loop:
    currentCoin = None
    closestGold = None
    minGoldDist = 9001
    coinIndex = 0
    coins = self.findItems()
    
    while coinIndex < len(coins):
        currentCoin = coins[coinIndex]
        if currentCoin.value = 3 and self.distanceTo(currentCoin) < minGoldDist:
                minGoldDist = self.distanceTo(currentCoin)
                closestGold = currentCoin
        coinIndex += 1
    
    if closestGold:
        self.moveXY(closestGold.pos.x, closestGold.pos.y)

Please enclose your code in three backticks as described in the FAQ, which you should read before posting.


Why do you think this is an error? As the coins are randomly distributed, it might happen that you pass a copper coin close enough to collect it.
Do you always get 30 coins when you hit submit or only sometimes? Do you by any chance collect the first copper coin also?

Well, to start with, the code is flawed. Since this is python, half of the if statement isn’t doing what it’s supposed to do (currentCoin.value = 3). If the goal of the level is to teach users to prioritize “gold,” then my code totally failed to accomplish the task that was intended.

Yes, I submitted the code multiple times and the flawed code routinely collected 30 coins (just as the correct code routinely collected 29 coins).

Thanks for the reminder.

pretty sure this should be

if currentCoin.value == 3

currently you are attempting to assign 3 to currentCoin.value not compare with it

Right. I understand that. And I fixed it in my final code. My issue was that I was able to “beat” the level with that flaw in my code – which essentially means that trying to prioritize the gold coins doesn’t actually have any bearing on winning the level. In fact, I just reran the level just with:

    while coinIndex < len(coins):
        currentCoin = coins[coinIndex]
        if self.distanceTo(currentCoin) < minGoldDist:
                minGoldDist = self.distanceTo(currentCoin)
                closestGold = currentCoin
        coinIndex += 1

And I still “won” the level – even though I clearly missed a number of the gold coins. In fact, I just tried again and I passed the level with the following stripped down code:

loop:    
    closestGold = self.findNearest(self.findItems())
    if closestGold:
        self.moveXY(closestGold.pos.x, closestGold.pos.y)

With the current level’s construction, if you have a fast enough hero, then the code doesn’t matter.

I’ve added a task to the level to see if we can make it more robust. It does account for hero speed, but perhaps it needs updating now with my changes to plan() making the hero actions more responsive.

1 Like