-1

I am new to Python and OOP in general and struggling to wrap my head around it. Few questions on the following problem...

  1. I want to be able to build a full deck of cards, and with an input of 'deck = Deck('♣')', only choose the cards of that suit from the deck. How would I be able to do that with one input? I'm currently trying to print 'test' for that situation in Deck().

  2. I would like to make sure inputs are validated in the PlayingCard() class however it doesn't seem to be doing its job.

  3. The current return function prints as a string if I execute 'deck = Deck()...print(deck)' however if I was to input 'print(deck.cards)', it would print the objects. I have tried various forms of str and repr to no success.

Your guidance/suggestions are appreciated!

import random

class PlayingCard():
    card_ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    card_suits = ['♠', '♥', '♦', '♣']

    def __init__(self, rank, suit):
        self.rank = str(rank)
        self.suit = str(suit)

    def invalid_d(self): #Not functioning....
        if self.suit not in self.card_suits:
            raise Exception("Pick a valid suit")
        if self.rank not in self.card_ranks:
            raise Exception("Pick a valid rank")

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

class Deck():
    cards = []
    card_ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    card_suits = ['♠', '♥', '♦', '♣']

    def __init__(self):
        self.build_deck()

    def build_deck(self):
        for i in self.card_ranks:
            for j in self.card_suits:
                self.cards.append(PlayingCard(j, i))
        return self.cards

        if self.suit != None:
            print('test')

    def __str__(self):
        return '[' + ', '.join(str(c) for c in self.cards) + ']'
anan
  • 1
  • 1
    For (1): You can add additional arguments to the `__init__` function of the class, e.g., `def __init__(self, suit)`. Then you can assign `self.suit = suit` before calling `self.build_deck()`. – Dillon Jun 26 '21 at 08:29

2 Answers2

1

You are asking too many questions and your code has too many problems.

I'm going to pick one problem and solve that:

class PlayingCard():
    card_ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    card_suits = ['♠', '♥', '♦', '♣']

    def __init__(self, rank, suit):
        self.rank = str(rank)
        self.suit = str(suit)
        self.invalid_d()   # call invalid_d() here

If you actually call invalid_d() in __init__() you would find one of the other problems in your code.

quamrana
  • 37,849
  • 12
  • 53
  • 71
1

Following quamrana's approach, let me pick another problem: if you want to have the option of creating a full deck, or just a deck of clubs, or maybe any combination of suits then you need to allow for a parameter in your Deck.__init__() and consequently Deck.build_deck() methods. (By the way, why do you need a separate build_deck() at all?) For instance:

class Deck():
    cards = []
    card_ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

    def __init__(self, card_suits = ['♠', '♥', '♦', '♣']):
        self.build_deck(card_suits)

    def build_deck(self, card_suits):
        for i in self.card_ranks:
            for j in card_suits:
                self.cards.append(PlayingCard(j, i))
        return self.cards

deck1 = Deck()               ##will give a full deck
deck2 = Deck(['♠', '♥'])     ##will give a deck with spades and hearts only

EDIT: Do not use a mutable sequence as argument default. Any immutable sequence will do. So the following line

    def __init__(self, card_suits = ['♠', '♥', '♦', '♣']):

should become one of

    def __init__(self, card_suits = ('♠', '♥', '♦', '♣')):

or

    def __init__(self, card_suits = '♠♥♦♣'):
gimix
  • 3,431
  • 2
  • 5
  • 21
  • 2
    +1 for your approach of just picking a problem, but you need to adjust the `card_suits` parameter in the light of this [problem](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – quamrana Jun 26 '21 at 09:44
  • Ooops. I was lazy and just copied/pasted the list from OP's code :( Editing the answer – gimix Jun 26 '21 at 09:56
  • Thank you, very helpful! Resolved a lot of the other problems as well! – anan Jun 27 '21 at 19:37