【发布时间】: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()吗? -
知道了,谢谢