Here is my code:
coinIndex = 0
loop:
coins = self.findItems()
coin = coins[coinIndex]
if coin.value == 3:
self.moveXY(coin.pos.x, coin.pos.y)
coinIndex += 1
else:
coinIndex += 1
pass
I can solve this level is a different fashion easily, but I was hoping to know why my character picks up 8 gold coins perfectly and then the “tmp26 is undefined” error comes up on my “if coin.value == 3” line. I’m not certain if I need to use a while loop to compare the coins to coinIndex. Any insights on what could be fixed on this code is appreciated.
@chale - Try replacing if coin.value == 3 with: if coin and coin.value == 3.
This checks for coin also. I think your loop was breaking because of unavailability of coin of value 3 and trying to pick it up.
Also notice that in my correction ‘if coin and coin.value == 3’, coin comes first and them coin.value. This is the order how the python compiler reads it. Only when coin is true, coin.value == 3 is checked. So, no errors for coin not being on the map.
What I suggested is equivalent to:
if coin:
if coin.value == 3:
self.moveXY (coin.pos.x, coin.pos.y)