0

So I'm currently having a problem with my Blackjack project. I am currently trying to create a class deck (Code below)

import random

class Cards:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
    def display_card(self):
        print("{} of {}".format(self.number,self.suit))
    def card_syntax(self):
        if self.number == 1:
            self.number = "Ace"
        if self.number == 11:
            self.number = "Jack"
        if self.number == 12:
            self.number = "Queen"
        if self.number == 13:
            self.number = "King"

        print("{} of {}".format(self.number, self.suit))

class Deck:
    def __init__(self):
        self.cards = []
        self.card_characterists()

    def card_characterists(self):
        for suit in ["Hearts", "Spades", "Clubs", "Diamonds"]:
            for number in range(1,14):
                self.cards.append(Cards(suit,number))

    def shuffle_cards(self):
        random.shuffle(self.cards)

    def draw(self):
        return self.cards.pop()

    def show(self):
        for c in self.cards:
            print(c)

The issue that I have is in regard to the show() method in the Deck class. I tried to use a FOR loop to get the objects.

def show(self):
    for c in self.cards:
        print(c)

I seem to get a result of:

<__main__.Cards object at 0x7f9269ed6390>
<__main__.Cards object at 0x7f9269ed6198>
<__main__.Cards object at 0x7f9269ed6358>
<__main__.Cards object at 0x7f9269ed6b00>
<__main__.Cards object at 0x7f9269ed6588>
<__main__.Cards object at 0x7f9269ed6710>
<__main__.Cards object at 0x7f9269ed6a58>
<__main__.Cards object at 0x7f9269ed6a20>
<__main__.Cards object at 0x7f9269ed68d0>
<__main__.Cards object at 0x7f9269ed62e8>
<__main__.Cards object at 0x7f9269ed6320>
<__main__.Cards object at 0x7f9269ed69e8>
<__main__.Cards object at 0x7f9269ed61d0>
<__main__.Cards object at 0x7f9269ed6978>
<__main__.Cards object at 0x7f9269ed60b8>
<__main__.Cards object at 0x7f9269ed6470>
<__main__.Cards object at 0x7f9269ed6ba8>
<__main__.Cards object at 0x7f9269ed6748>
<__main__.Cards object at 0x7f9269ed62b0>
<__main__.Cards object at 0x7f9269ed6828>
<__main__.Cards object at 0x7f9269ed6be0>
<__main__.Cards object at 0x7f9269ed6780>
<__main__.Cards object at 0x7f9269ed65f8>
<__main__.Cards object at 0x7f9269ed6ac8>
<__main__.Cards object at 0x7f9269ed6400>
<__main__.Cards object at 0x7f9269ed6668>
<__main__.Cards object at 0x7f9269ed64e0>
<__main__.Cards object at 0x7f9269ed64a8>
<__main__.Cards object at 0x7f9269ed69b0>
<__main__.Cards object at 0x7f9269ed66d8>
<__main__.Cards object at 0x7f9269ed6c18>
<__main__.Cards object at 0x7f9269ed6898>
<__main__.Cards object at 0x7f9269ed6630>
<__main__.Cards object at 0x7f9269ecbfd0>
<__main__.Cards object at 0x7f9269ed67b8>
<__main__.Cards object at 0x7f9269ed65c0>
<__main__.Cards object at 0x7f9269ed66a0>
<__main__.Cards object at 0x7f9269ed6860>
<__main__.Cards object at 0x7f9269ed6908>
<__main__.Cards object at 0x7f9269ed6550>
<__main__.Cards object at 0x7f9269eb64e0>
<__main__.Cards object at 0x7f9269ed6a90>
<__main__.Cards object at 0x7f9269ed63c8>
<__main__.Cards object at 0x7f9269ed6940>
<__main__.Cards object at 0x7f9269ecb710>
<__main__.Cards object at 0x7f9269ed6518>
<__main__.Cards object at 0x7f9269ed67f0>
<__main__.Cards object at 0x7f9269ed6080>
<__main__.Cards object at 0x7f9269ed60f0>
<__main__.Cards object at 0x7f9269ed6b38>
<__main__.Cards object at 0x7f9269ed6438>

Is there a way to actually get a readable result from self.cards(). This is my first post so I hope you don't mind if this has been badly phrased.

  • 1
    If this is a question about python, then it would be helpful to add the python tag. If it is python, this might help: https://stackoverflow.com/questions/3204614/how-to-change-any-data-type-into-a-string-in-python. (In particular `def __str__(self):` etc.) – heijp06 Oct 06 '20 at 12:19
  • Yes, define an `__str__` method in your class and return a string as you like it to be. – progmatico Oct 06 '20 at 19:32

1 Answers1

0

Add __str__ to Cards.

Read more.

See below

class Cards:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
    def __str__(self):
        return f'suit: {self.suit}, number: {self.number}'
balderman
  • 22,927
  • 7
  • 34
  • 52