0

I am pretty new to Python and in an attempt to learn, I decided to code a project which includes the making of a deck of cards (it's blackjack, I guess many people do this when learning). Simply put, the code recursively makes all the cards based on the defined cards class.

However, when I want to call the list and check that the cards are actually in the correct order (by calling a random element and calling the description method) I get an error.

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

    def description(self):
        return f"{self.value} of {self.suit}"

values = ["ace", 2,3,4,5,6,7,8,9,10,"jack","queen","king"]
colors = ["Hearts","Diamonds","Spades","Clubs"]
deck = [card(value,color) for value in values for color in colors]
deck[3].description()

I would expect to get the description of the fourth instance of the cards created. However, I get the following error:

<ipython-input-56-567b42634c58> in <module>
     12 c = Card("ace","Spades")
     13 c.description()
---> 14 deck[3].description()

AttributeError: 'card' object has no attribute 'description'

I don't why there is an error, the code makes instances of the class so I would expect to be able to call the method/attribute (actually it says .description is an attribute but isn't it a method? I don't quite get the difference...)

I couldn't find someone with a similar problem and looking up this error in general doesn't help my either. If there is a similar question out there, excuse my redundancy! Is there a simple mistake I am making? Any comment would be greatly appreciated. Thanks in advance!

2
  • 8
    Your class is called Card but you're setting that list with card which is probably something else in your code, and you error shows lines of code not in your code example Commented Nov 10, 2019 at 6:10
  • Good catch, Kinda embarrassing... Commented Nov 10, 2019 at 6:18

1 Answer 1

1

Your class is called Card but you're setting that list with card which is probably something else in your code, and you error shows lines of code not in your code example.

Credit to Ofer Sadan in the comments. Formalizing it into an answer.

Sign up to request clarification or add additional context in comments.

2 Comments

When the problem is caused by a typo, the question ought to be closed.
@kaya3 under the "Flag" settings, which should be used for closing based on typo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.