当我运行代码时,第一次打印出套牌,但第二次打印“无”。我认为套牌清单由于某种原因被覆盖,但无法弄清楚原因。任何帮助将不胜感激。
class Deck(object):
def __init__(self,deck=['2c','3c','4c','5c','6c','7c','8c','9c','tc','jc','qc','kc','ac','2d','3d','4d','5d','6d','7d','8d','9d','td','jd','qd','kd','ad','2h','3h','4h','5h','6h','7h','8h','9h','th','jh','qh','kh','ah','2s','3s','4s','5s','6s','7s','8s','9s','ts','js','qs','ks','as']):
self.deck=deck
def shuffle_cards(self):
self.deck=random.shuffle(self.deck)
l=Deck()
print l.deck
l.shuffle_cards()
print l.deck
答案 0 :(得分:0)
shuffle就位(https://docs.python.org/2/library/random.html#random.shuffle)
random.shuffle(self.deck)
将取代
self.deck=random.shuffle(self.deck)
btw:mutable默认参数值不完美