2

Upon print(player), I want to display a list of cards to my user. The "output", is a list, but of the locations of the card object. I have already defined __ str__ in the Card class. Hence, I am unable to understand why this is not working or if there is some other way to display the required.

suits = ["clubs","spades","hearts","diamond"]
ranks = ["two", "three","four","five", "six","seven","eight", 
         "nine","ten","jack","queen","king","ace"]

class Card:

def __init__(self,rank,suit):
    
    self.rank = rank
    self.suit = suit
    self.value = values[rank]
    
def __str__(self):
    return f"{self.rank} of {self.suit}. Value: {self.value}"


class Deck:
    
def __init__(self):

    li_deck_cards = []

    for suit in suits:
        for rank in ranks:
            li_deck_cards.append(Card(rank,suit))
            
    self.deck_cards = li_deck_cards
def deal_card(self):
    return self.deck_cards.pop()
            
def __str__(self):
        return f"Has {len(self.deck_cards)} Cards"

class Player:
    
def __init__(self,bankroll=0.00):
    
    li_current_cards = []
    
    self.bankroll = bankroll
    self.current_cards = li_current_cards

def __str__(self):
    return f"{self.current_cards}"


player = Player()
deck = Deck()
player.current_cards.append(deck.deal_card())
print(player)

**Have posted only selected relevant code, feel free to ask for missing pieces if required.

er-ads
  • 23
  • 3
  • 1
    You are seeing the *default representation of an object* because `list` uses the `__repr__` of an object for building it's own `__str__` and `__repr__` representations. – juanpa.arrivillaga Oct 16 '22 at 05:21

0 Answers0