I am building a variation of poker that my family and I play and I’m trying to see if there is a way to quickly create card objects with variable names that are intuitively referenced when playing a hand. This is what I have so far:
numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace']
suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
class Card:
def __init__(self, suit, number):
self.suit = suit
self.number = number
if self.suit == 'Spades':
suit_rank = 1
elif self.suit == 'Hearts':
suit_rank = 2
elif self.suit == "Clubs":
suit_rank = 3
elif self.suit == 'Diamonds':
suit_rank = 4
self.suit_rank = suit_rank
def __repr__(self):
description = "{number} of {suit}".format(number = self.number, suit = self.suit)
return description
class Deck:
def __init__(self):
self.cards = []
for suit in suits:
for num in numbers:
self.cards.append(Card(suit, num))
def show(self):
for c in self.cards:
print (c)
The problem is these card objects are not easily called without knowing the index of where they fall in a player’s hand. I would like each cards object to be assigned to a variable that is named in such a way that the players can reference them by typing out something like “two of hearts.”
So I guess my question is, is there a way to loop through the list of numbers and suits to simultaneously create the objects and assign the variable name so that the objects are intuitively called? It seems tedious to have to go through and create each variable one by one.
I hope my question makes sense!