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!
Cardbut you're setting that list withcardwhich is probably something else in your code, and you error shows lines of code not in your code example