Python: AttributeError

import random

class Deck:
  def ___init__(self):
    self.cards = []
    suits = ["spades", "clubs", "hearts", "diamonds"]
    ranks = [{"rank": "A", "value": 11},
            {"rank": "2", "value": 2},
            {"rank": "3", "value": 3},
            {"rank": "4", "value": 4},
            {"rank": "5", "value": 5},
            {"rank": "6", "value": 6},
            {"rank": "7", "value": 7},
            {"rank": "8", "value": 8},
            {"rank": "9", "value": 9},
            {"rank": "J", "value": 10},
            {"rank": "K", "value": 10},
            {"rank": "Q", "value": 10}]
    for suit in suits:
      for rank in ranks:
        self.cards.append([suit, rank])

  def shuffle(self):
    random.shuffle(self.cards)
  def deal(self,number):
    cards_dealt = []
    for x in range(number):
      card = self.cards.pop()
      cards_dealt.append(card)
    return cards_dealt

deck1 = Deck()
print(deck1.cards)

this is my code.
And this is the error:
Traceback (most recent call last):
File “main.py”, line 33, in
print(deck1.cards)
AttributeError: type object ‘Deck’ has no attribute ‘cards’

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

help me to fix this code

I’m sure you didn’t mean it as such, but that is very rude and demanding. You aren’t my boss, so it’s not polite for you to give me orders.

I’d guess you aren’t invoking the constructor correctly.

I’m really sorry sir, i think it was a bot. If it hurts forgive me for this.
Can you please help me to fix this error ?

I would double check how objects are created in Python.

There you go. Typo in the function name.

means, i didn’t understand sir.

The name you gave to your init function is wrong.

Thank you sir Now it is fix :heart_eyes: :heart:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.