0
from enum import Enum
from enum import IntEnum

class Suits(Enum):
    DIAMONDS="diamonds"
    HEARTS="hearts"
    SPADES="spades"
    CLUBS="clubs"


class Values(IntEnum):
    TWO=2
    THREE=3
    FOUR=4
    FIVE=5
    SIX=6
    SEVEN=7
    EIGHT=8
    NINE=9
    TEN=10
    JACK=11
    QUEEN=12
    KING=13
    ACE=14


deck_of_cards=[]


class Playing_Cards():
    def __init__(self, card_value, card_suit):
        self.card_value=card_value
        self.card_suit=card_suit

deck_of_cards=[]

for suit in Suits:
    for value in Values:
        deck_of_cards.append(Playing_Cards(value,suit))

print(deck_of_cards)

I want to print deck_of_cards and it's showing a list of objects. Could you please explain why?

[<__main__.Playing_Cards object at 0x102268128>,
<__main__.Playing_Cards object at 0x102268198>,
<__main__.Playing_Cards object at 0x1022681d0>, <__main__.Playing_"

Questions:

  1. Did I add cards(value, suit) to the list properly?
  2. How can I print what is in the list deck_of_cards?
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Give your Playing_Cards class `__str__` and `__unicode__` methods. – g.d.d.c May 25 '18 at 21:13
  • it's just printing to the address of the object, look in to a different way of printing a list together, either as a string using `join` or looping through each entry in to the list and printing it – James Lingham May 25 '18 at 21:13
  • Python has no idea what you want to happen when you print a `Playing_Cards` instance, unless you tell it. It defaults to that `` just to have _something_ to print out. If you want something different, define a `__repr__` method and/or a `__str__` method. – abarnert May 25 '18 at 21:13
  • 1
    Nominated for reopening as dupe question is about custom string representation of *classes* not *objects*. The solution to each is different enough to warrant different answers. Looking for another suitable answer. – Dunes May 25 '18 at 21:45
  • Generally, you should avoid `IntEnum` unless you are working with legacy code... – juanpa.arrivillaga May 26 '18 at 02:03
  • Also, the class should be PlayingCard, singular, since one instance represents a single card. – Lee Daniel Crocker May 29 '18 at 20:11

0 Answers0