Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 我想显示我的卡的列表,但出现错误_Python 2.7 - Fatal编程技术网

Python 2.7 我想显示我的卡的列表,但出现错误

Python 2.7 我想显示我的卡的列表,但出现错误,python-2.7,Python 2.7,每当我想显示我的卡片时,它就会给我错误信息 我想把它显示为列表 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): retu

每当我想显示我的卡片时,它就会给我错误信息

我想把它显示为列表

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>]
类卡:
西装=[“H”、“D”、“C”、“S”]
值=[“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“T”、“J”、“Q”、“K”、“A”]
定义初始(自我、价值、适合):
自我价值,自我适应=价值,适应
定义(自我):
返回self.values[self.value]++of“+self.suits[self.suits]
舱面:
定义初始化(自):
self.decks=[]
对于范围(13)内的x:
对于范围(4)内的y:
自我组。附加(卡(x,y))
定义(自我):
返回自助甲板
def打印卡(自):
打印自助卡
def main():
n=甲板()
n、 打印卡()
输出:
[, , 
, ]

打印
self.deck
打印
对象的列表

由于您没有为
卡片
对象定义
\uuuuuuuuu repr\uuuuuuu
方法(
\uuuuuuuuu str\uuuuu
方法仅用于转换为
字符串
,但在表示对象的
列表
时不使用转换为字符串,因此只能用于
打印(self.deck[0])
),您只需获得默认表示:对象地址

由于您没有提供
类,因此我创建了一个模型,并定义了适当的
\uuuu repr\uuuu

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]

打印
self.deck
打印
Cards
对象的列表

由于您没有为
卡片
对象定义
\uuuuuuuuu repr\uuuuuuu
方法(
\uuuuuuuuu str\uuuuu
方法仅用于转换为
字符串
,但在表示对象的
列表
时不使用转换为字符串,因此只能用于
打印(self.deck[0])
),您只需获得默认表示:对象地址

由于您没有提供
类,因此我创建了一个模型,并定义了适当的
\uuuu repr\uuuu

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]

您还必须定义
类的表示形式。您还必须定义
类的表示形式。现在没事了。我已经显示了我的牌组列表谢谢:)这看起来像是一个新问题。也许你可以编辑你的问题来添加它(使用编辑:标题)?(更准确一点,因为那样的话它很宽泛)哦,对不起,再次感谢:)现在没事了。我已经展示了我的套牌清单谢谢:)这看起来像是一个新问题。也许你可以编辑你的问题来添加它(使用编辑:标题)?(更准确一点,因为那样的话它很宽泛)哦,对不起,再次谢谢你:)