I'm making 10 coin objects from the class MakeCoin. I put all the coin objects on the coinlist[].
I've also made a method in class MakeCoin, named pickup, this class will delete the object from the coinlist[].
In the second last part of the program I iterate over the coinlist[] with coin, and I delete the coin object name from the coinlist[] with the method pickup(). It is deleting coins from the coinlist[] but still 5 coin object names remain in the list (the even ones) - I really don't understand why they stay on the list, and how can I delete the whole list ?
from random import randint
class MakeCoin(object):
def __init__(self,sprite):
#give the coin random x,y location
self.x=randint(0,10)
self.y=randint(0,10)
self.sprite=sprite
def show(self):
#show the coin location
print "x:%d y:%d" %(self.x,self.y)
def pickup(self):
#you've picked up this coin, so delete it from list
coinlist.remove(self)
#generate 10 coins
coinlist=[]
for x in range(0,10):
coinlist.append(MakeCoin(1))
#this will show that there are 10 coins in list
print len(coinlist)
#this will let you pickup all the coins from the list(remove coins from coinlist)
for coin in coinlist:
#delete the coin !
coin.pickup()
#in my opinion, this should print out 0 ...but it say's 5 ! (there -
#are still five coins on the list, the evencoins are still there...how to solve this ?
print len(coinlist)