1

Alright, started learning python a while back, worked myself up to learning about OOP in general, then they tell me to write up a game of blackjack. Now, I made objects for every card, put all those objects in a list to simulate a deck. I'm having trouble to figure out how to cleanly return one specific attribute from every item in that list.

I originally just tried to print, but then I realised I thought the thing was smarter than it actually is, which was a problem. Python's a good language, and I'm sure the tools are there. I just don't know it yet

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

ACES = Card("Ace","Spades",10)
TWOS = Card(2,"Spades",2)
THRS = Card(3,"Spades",3)
FOUS = Card(4,"Spades",4)
FIVS = Card(5,"Spades",5)
SIXS = Card(6,"Spades",6)
SEVS = Card(7,"Spades",7)
EIGS = Card(8,"Spades",8)
NINS = Card(9,"Spades",9)
TENS = Card(10,"Spades",10)
JACS = Card("Jack","Spades",10)
QUES = Card("King","Spades",10)
KINS = Card("Queen","Spades",10)

deck = [ACES,TWOS,THRS,FOUS,FIVS,SIXS,SEVS,EIGS,NINS,TENS,JACS,QUES,KINS]

print (deck.face)

In hindsight I should've realised the code would think I demanded an attribute from the list itself, not the items inside, but I'm stumped.

1
  • You will have to get a Card object by iteratign the list (for card in deck:) or by using an index (card = deck[0]), then you can access the attributes of it. Commented Jul 22, 2019 at 11:00

5 Answers 5

1

In hindsight I should've realised the code would think I demanded an attribute from the list itself, not the items inside

Actually, code doesn't "think", it is only executed. And the runtime doesn't "think" either, it just executes the code provided. But those minor conceptual nuances set aside, your diagnostic is right: deck.face looks up the attribute named "face" on the deck (list) object itself.

If your question is "how to print (or collect or do whatever with) the face attribute for each card in deck, the simple solution is to iterate on the list:

for card in deck:
    print(card.face)

or if you want to build a list of face values:

faces = [card.face for card in deck]

Now if it's an exercise in OO design, you may want to reconsider the use of a list to represent a "deck" object - a cards deck is actually not a list.

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

Comments

0

Since each element in deck list is Card Object, hence you need to iterate the list and get the face attribute as given below :

faceList = []
for obj in deck:
  faceList.append(obj.face)

print (faceList)

Comments

0

You could easily use list-comprehension:

deck = [ACES,TWOS,THRS,FOUS,FIVS,SIXS,SEVS,EIGS,NINS,TENS,JACS,QUES,KINS]

deck_faces = [card.face for card in deck]
print(deck_faces)

And this gives:

['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'King', 'Queen']

Comments

0

I think a good solution would be using dictionaries, a dictionary allows you to keep even a string as a key for traversing a list, this is what you could do

cards_dict={"ACES": Card("Ace","Spades",10), "TWOS": Card(2,"Spades",2)}

and so on, you could then access the "ACES" card by directly querying it like this: cards_dict["ACES"]

or you could do this

cards_dict = {d.face: d for d in deck}

then you coudl directly query a card using the face name, as such: cards_dict["Ace"]

4 Comments

The way I set it up is that I use a random number to determine what card to pull from the list, a dict doesn't really allow that
Hi @Lurcolm, why don't you add that in the question? That could actually change some of the answers...
You could theoretically still work with dicts, list(dict.keys()) gives you all the keys in list form, you may then sample a random index number and index it via they keys list to get a random card
Or in that case just sample the keys list and then access the dict with that random key...
0

just use a for loop for all of them

for card in deck:
    print(card.face)

or

deck[0].face

for specific item

Comments

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.