import random
class card:
suit = ['♥','♦','♣','♠']
card = ['K','Q','J',10,9,8,7,6,5,4,3,2,'A']
class deck(card):
def __init__(self):
self.d1 = []
for x in self.suit:
for y in self.card:
self.d1.append(x str(y))
def mix(self):
random.shuffle(self.d1)
def pop(self):
return self.d1.pop()
d = deck()
#print(d.d1)
#d.mix()
print(d.d1)
for x in d.d1:
print(d.d1.pop())
print(d.d1)
for each loop in the code only pop()
out half the elements in the list. i.e. if 52 cards are in the loop only 26 are getting pop()
out of the list
CodePudding user response:
You are removing out of the same list you are iterating over. quickest change is this:
for x in list(d.d1):
print(d.d1.pop())
CodePudding user response:
it is because "pop method of the list" changes the list and list size. While when we use it like
for i in range(0, len(d.d1)):
d.pop()
or
while True:
try:
d.pop()
except:
break
it is ok.