每当我想显示我的卡时,它就会给我错误。
我想将其显示为列表。
class Card:
suits = ["H", "D", "C", "S"]
values = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"]
def __init__(self, value, suit):
self.value, self.suit = value, suit
def __str__(self):
return self.values[self.value] + " of " + self.suits[self.suit]
class Deck:
def __init__(self):
self.decks = []
for x in range(13):
for y in range(4):
self.decks.append(Cards(x, y))
def __str__(self):
return self.decks
def printcard(self):
print self.decks
def main():
n=Deck()
n.printcard()
Output:
[<__main__.Card instance at 0x0000000002694388>, <__main__.Card instance at
0x00000000026944C8>, <__main__.Card instance at 0x0000000002697C08>,
<__main__.Card instance at 0x0000000002697C48>, <__main__.Card instance at
0x0000000002697C88>, <__main__.Card instance at 0x0000000002697CC8>]
答案 0 :(得分:0)
打印Cards
打印__repr__
个对象的列表。
由于您没有为Cards
对象定义__str__
方法(string
方法仅对转换为list
有用,但转换为字符串不是在表示print(self.decks[0])
个对象时使用,仅适用于Cards
),您只需获得默认表示:对象地址。
由于您没有提供__repr__
课程,因此我创建了一个具有正确class Card:
suits = ["H", "D", "C", "S"]
values = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"]
def __init__(self, value, suit):
self.value, self.suit = value, suit
def __repr__(self):
return self.values[self.value] + " of " + self.suits[self.suit]
class Deck:
def __init__(self):
self.decks = []
for x in range(13):
for y in range(4):
self.decks.append(Card(x, y))
def printcard(self):
print(self.decks)
n=Deck()
n.printcard()
定义的模型:
[2 of H, 2 of D, 2 of C, 2 of S, 3 of H, 3 of D, 3 of C, 3 of S, 4 of H, 4 of D, 4 of C, 4 of S, 5 of H, 5 of D, 5 of C, 5 of S, 6 of H, 6 of D, 6 of C, 6 of S, 7 of H, 7 of D, 7 of C, 7 of S, 8 of H, 8 of D, 8 of C, 8 of S, 9 of H, 9 of D, 9 of C, 9 of S, T of H, T of D, T of C, T of S, J of H, J of D, J of C, J of S, Q of H, Q of D, Q of C, Q of S, K of H, K of D, K of C, K of S, A of H, A of D, A of C, A of S]
现在我按照预期得到了这个结果:
<table>