class Deck:
def __init__(self):
self.suits = ['s', 'h', 'd', 'c']
self.ranks = ['6' ,'7', '8', '9', '10', 'J', 'Q', 'K', 'A']
self.deck = [i+j for i in self.ranks for j in self.suits]
random.shuffle(self.deck)
def selectCards(self):
self.selectedCard = self.deck.pop()
return self.selectedCard
def respresentDeck(self, dck):
symbols = { 'c': u"\u2663", 'h': u"\u2665", 's': u"\u2660", 'd': u"\u2666"}
tempL = [x for sublist in dck for x in sublist]
representList = [symbols[i] + ' ' if i in symbols else i for i in tempL]
representList = list('Deck: ') + representList
return u"".join(representList).encode('utf-8').strip()
def __str__(self):
return self.representDeck(self.deck)
class Game:
newDeck = Deck()
print(newDeck)
我正在尝试以ascii格式表示卡片,但收到错误return self.representDeck(self.deck)
AttributeError: Deck instance has no attribute 'representDeck'
我有Player
类非常相似的代码,这一切都很好,但事实并非如此。
答案 0 :(得分:2)
你拼错了represent
:
>>> Deck.respresentDeck.__name__ == 'representDeck'
False
那是因为其中有s
太多了:
>>> print '\n'.join(['representDeck', Deck.respresentDeck.__name__])
representDeck
respresentDeck
# ^
更正方法的名称:
def representDeck(self, dck):
# ...
答案 1 :(得分:1)
你在__str__
方法拼错了这个词,它与方法名称不匹配:
self.respresentDeck(self.deck)
但实际上你的方法应该是representDeck
,因为这是该单词的正确拼写。
所以def representDeck(self, dck):
和return self.representDeck(self.deck)
。