0

I've just started learning to program and am now attempting to write a blackjack program. A problem I've come across is in my way of representing a deck, here's how I did it (the suit doesn't matter to me):

A = 11
J = 10
Q = 10
K = 10

deck = [A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K
    ] * 4

The problem is when face cards are 'dealt' they show a 10 (11 for A..) when I'd like them to show as J or Q or K or A.

Here's the deal function:

def deal(competitor, x):
    for i in range (0,x):
        card = shoe[0] 
        dealt.append(card)
        competitor.append(card)
        shoe.remove(card)

Any fixes?

1 Answers1

4

First, the easiest way to do this sort of mapping is with a dictionary. Second, your deck as it stands is going to run into a common problem with multiplying lists. You can represent the deck and look up face cards with the following:

face_map = {'A':11, 'J':10, 'Q':10, 'K':10}
deck = list('A23456789JQK'*4)

With this new deck and face_map, you can look up the value of any given card with face_map.get(card) or int(card), which will return the retrieved value for any face card, or the integer value of non-face cards. Here's a demo:

>>> print(*(face_map.get(card) or int(card) for card in 'A23456789JQK'))
11 2 3 4 5 6 7 8 9 10 10 10
Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97