【问题标题】:AttributeError: type object 'Deck' has no attribute 'cards'AttributeError: 类型对象 \'Deck\' 没有属性 \'cards\'
【发布时间】:2022-12-12 14:12:32
【问题描述】:

我正在尝试编写一个 python oop 纸牌游戏,但出现此错误,该错误表明类型对象“Deck”没有属性“cards”

编码 :

class Card:
    def __init__(self, name, suit):
        self.name = name
        self.suit = suit

    def print_card(self):
        print(self.name, self.suit)


class Deck:
    def __init__(self):
        self.cards = []

        names = ("A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2")
        suits = ("D", "C", "H", "S")

        for name in names:
            for suit in suits:
                card = Card(name, suit)
                self.cards.append(card)


deck = Deck
for card in deck.cards:
    card.print_card()

错误 :

Traceback (most recent call last):
  File "/Users/yoshithkotla/PycharmProjects/pythonFinalProject001/main.py", line 30, in <module>
    for card in deck.cards:
AttributeError: type object 'Deck' has no attribute 'cards'

Process finished with exit code 1

【问题讨论】:

  • 会相信这是deck = Deck()吗?
  • 知道了,谢谢

标签: python oop


【解决方案1】:

我相信是deck = Deck()

您所拥有的只是将 deck 设置为类 Deck 而不是实例化它。

【讨论】:

    【解决方案2】:
    deck = Deck()
    

    用于创建一个对象 ofDeck

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2021-11-09
      • 2019-06-08
      • 2022-01-12
      • 2016-11-18
      • 2019-02-25
      • 2021-09-01
      相关资源
      最近更新 更多