Home > Net >  AttributeError: type object 'Deck' has no attribute 'cards'
AttributeError: type object 'Deck' has no attribute 'cards'

Time:12-12

I am trying to code a python oop card game and I get this error, the error says the type object 'Deck' has no attribute 'cards'

THE CODE :

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()

THE ERROR :

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

CodePudding user response:

I would believe it's deck = Deck()

What you have is just setting deck to be the class Deck and not instantiating it.

CodePudding user response:

deck = Deck()

for creating an object of Deck

  • Related