I am new to Python and OOP in general and struggling to wrap my head around it. Few questions on the following problem...
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().
I would like to make sure inputs are validated in the PlayingCard() class however it doesn't seem to be doing its job.
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) + ']'