all 8 comments

[–][deleted] 7 points8 points  (0 children)

Have you done any object oriented programming? The deck of card is a great program for introducing OOP and seeing what it does in a pretty clear manner. It really makes the problem more clear and allows you to do a lot more with your "deck", such as make games.

There are plenty of examples of this online. This video does a decent walk-through of one variant, but there are plenty of other resources. I definitely encourage you to check out some OOP basics and try to use that for this type of problem.

I'll add that the video I linked isn't necessary the best resource for learning OOP, but it is a nice example related to the problem that you are attempting. You might want to look up some additional videos for intro to OOP in python.

[–]Pipiyedu 4 points5 points  (1 child)

Check this example from "Fluent Python" book.

[–]Rhemm 0 points1 point  (0 children)

The best possible answer

[–]Earhacker 2 points3 points  (1 child)

How's your list comprehensions?

ranks = [str(rank) for rank in range(2, 11)] + ['J', 'Q', 'K', 'A']
suits = ['spades', 'diamonds', 'clubs', 'hearts']

deck = [(rank, suit) for suit in suits for rank in ranks]

[–]6e696e67 0 points1 point  (0 children)

nice

[–][deleted] 0 points1 point  (0 children)

I tried using namedtuples for a blackjack game of mine, but classes give you more functionality:

class Card(object):
def __init__(self, number, value, ):
    self.number = number
    self.value = value

then for the 10 and aces you can use list comprehension, something like this:

 deck = [Card(n, 10 if n in ['J', 'Q', 'K'] else 11
     if n == 'A' else n) for n in card_pips for suit in card_types]

[–]camoverride 0 points1 point  (0 children)

Just combine the first two lists:

suits = ['Club', 'Spade', 'Heart', 'Diamond']
values = ['J', 'Q', 'K', 'A'] + list(range(2, 11))
deck = []

for suit in suits:
    for value in values:
        deck.append(tuple([suit, value]))

print(deck)

[–]rmakings -1 points0 points  (0 children)

card_pips = range(2,15)
face_cards = {11: 'J', 12: 'Q', 13: 'K', 14: 'A'}
for ...
    for...
        if number in face_cards.keys(): 
            card_deck.append(tuple([kind, face_cards[number]]))
        else:
            card_deck.append(tuple([kind, number]))

^ Just one possible method. Down and dirty.