0

Firstly, thank you for taking the time to consider my question - I've included a lot of code, I certainly appreciate your support!

I have written two classes:

PlayingCard and Deck

I would like to print the attribute [cards] form the method [shuffle_deck] from the class [Deck]. I have used the str magic method in the class which allows me to correctly output:

deck2 = Deck('♠') > deck2.shuffle_deck() > print(deck2)

but it does not work when I try to print a list directly from the method:

print(deck2.cards)

I've watched several youtube videos but am still struggling to understand why this is happening. I've read many posts on this forum, but I can't find a solution that particularly focuses on the contrast between printing the class object versus a list within a class method.

Once again, thank you for your help!


class PlayingCard():
    def __init__(self,value,suit):

        if value in ['2','3','4','5','6','7','8','9','10','J','Q','K','A'] or value in [2,3,4,5,6,7,8,9,10,'J','Q','K','A']:
            self.rank = str(value)
            
        else:
            print("Invalid rank!")
            raise Exception("Invalid rank!")
        
        if suit in '♥♦♣♠':
            self.suit = suit
        else:
            print('Invalid suit!') 
            raise Exception('Invalid suit!')

    def get_rank(self):
        return self.rank

    def get_suit(self):
        return self.suit
    
    def __str__(self):
        return self.rank+" of " +self.suit


class Deck():
    ranks=[2,3,4,5,6,7,8,9,10,'J','Q','K','A']
    deck_cards=[]
    def __init__(self,suit = ['♥','♦','♣','♠']):
        self.suits = suit
        for rank in Deck.ranks:
            for suit in self.suits:
                card = PlayingCard(rank,suit)
                Deck.deck_cards.append(card)
        self.cards=[]
        for card in Deck.deck_cards:
            if card.get_suit() == suit:
                self.cards.append(card)

    def shuffle_deck(self):
        random.shuffle(Deck.deck_cards)

    def deal_card(self,count):
        deal_cards = []
        if count <= len(self.cards):
            deal_cards = random.sample(self.cards,count)
            for card in deal_cards:
                self.cards.remove(card)
            deal_card_str=''
            for card in deal_cards:
                deal_card_str += card.__str__()+", "
            print(deal_card_str[:-2])
        else:
            print('Cannot deal '+str(count)+' cards. The deck only has '+str(len(self.cards))+' cards left!')

    def __str__(self):
        full_deck=''
        for card in Deck.deck_cards:
            full_deck += card.__str__()+", "
        return full_deck[:-2]
    
    
deck2 = Deck('♠')
deck2.shuffle_deck()
print("I want to change this output to match the lower output", deck2.cards)
print("This is the correct format", deck2)```

1 Answers1

0

It's not working because deck2.cards is a list, so you'll need to iterate through that, like this:

deck2 = Deck('♠')
deck2.shuffle_deck()
print("I want to change this output to match the lower output"
print("{:s}".format(', '.join(deck2.cards)))
print("This is the correct format", deck2)

(Similarly to what you've done in the Deck class). This makes sense because each PlayingCard is a class instance of a single card, so there is no way for it to store the information on multiple cards, except in the way that you've already done it. In other words, the str function of PlayingCard only has access to its own data.

jhso
  • 3,103
  • 1
  • 5
  • 13