I am a total newbie to programming. I was hoping to learn how to write a program that prints out a string representation of a random card from a deck of playing cards. The programs should use either “A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, or “K” for the value of the card and either “c”, “h”, “s”, “d” for the suit. So, if the program randomly selects the jack of clubs, it should display Jc. Using
one tuple to represent all of the possible card values and another tuple for the possible suits. Thanks

I use 'T' instead of '10', but you can change this as needed

import random
rank = random.choice( ('A','2','3','4','5','6','7','8','9','T','J','Q','K') )
suit = random.choice( ('c','d','h','s') )
card = rank + suit
print card  # or print(card) for Python >= 2.6
commented: nice solution! +11

Nice solution by BearofNH.
Just a note, you can use print(card) with Python25 too.

Another idea that might be useful is to start with one list of all the unused cards in the deck (AC, 2C, 3C, etc). Randomly select cards from the list and put them into another list called used_cards and remove them from the list of unused cards.

I'm not sure what the OP is trying to do, so it might not matter, but the code from BearofNH could return the same card more than once. Not such a big deal if you're playing 21 against the computer, but that would be a problem if you have a whole table of blackjack players and five aces show up. It would completely wreak havoc on hearts.

One thing I would avoid is creating the list of unused cards like I mentioned above, getting a random sample and putting it into a list of used cards like I mentioned, but not removing it from the master list. If you're playing a game the distributes every card in the deck you might find your performance blows near the end of the deck because your random sample keeps pulling a card that is already in use and has to do the sample again.

I also suppose that you don't need to maintain the list of used cards because everything that isn't in the list of unused cards is obviously in use. I suggested doing it that way because when it gets to the end of the hand it would be really easy to put those cards back in the deck: unused_card_list += used_card_list and used_card_list = [].

unused_cards = ['AS', '2S', '3S', etc...]
used_cards = []
player_1_hand = []
player_2_hand = []


# deal five cards to player 1
for i in range(5):
    drawn_card = random.choice(unused_cards)
    used_cards.append(drawn_card)
    unused_cards=[f for f in unused_cards if f not in used_cards]
    player_1_hand.append(drawn_card)

# repeat the process for player 2
for i in range(5):
    drawn_card = random.choice(unused_cards)
    used_cards.append(drawn_card)
    player_2_hand.append(drawn_card)
    unused_cards=[f for f in unused_cards if f not in used_cards]

print player_1_hand
print player_2_hand

I like using random.shuffle() on the unused cards (or deck) and then you can just pop() a random card off the unused list.

If you need to keep a used cards list, you can append the card you just got.

When it is time to shuffle again, you can refill the unused cards and random.shuffle() again. This would also work with more than one deck of cards if it was desired.

I too used random.shuffle() after creating a deck, then pop the cards out as you would deal them in a real card game.

import random
#creating the deck of cards
cards = ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S',\
         'AD', 'KD', 'QD', 'JD', '10D', '9D', '8D', '7D', '6D', '5D', '4D', '3D', '2D',\
         'AC', 'KC', 'QC', 'JC', '10C', '9C', '8C', '7C', '6C', '5C', '4C', '3C', '2C',\
         'AH', 'KH', 'QH', 'JH', '10H', '9H', '8H', '7H', '6H', '5H', '4H', '3H', '2H']

#for this example I have 2 hands
hand1 = []
hand2 = []
#shuffle the cards
random.shuffle(cards)
num = input('How many cards to deal to each player? ')
while num > 0:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        num = num - 1
print 'hand one is: '
print hand1
print 'hand two is: '
print hand2
print 'the cards remaining in the deck are: '
print cards

Mildly more 'pythonic' ...

# an example of a deck of cards and drawing a random hand

import random

def new_deck():
    """
    create a deck of cards
    suit: club=C, diamond=D, heart=H spade=S
    rank: ace=A, 10=T, jack=J, queen=Q, king=K, numbers=2..9
    ace of spade would be AS, 8 of heart would be 8H and so on
    return a list of a full deck of cards
    """
    rs = [rank + suit for rank in "A23456789TJQK" for suit in "CDHS"]
    return rs

def draw_cards(n, cards_list):
    """
    randomly draw n cards from the deck (cards_list)
    remove those cards from the deck
    since object cards_list is by reference, it will change too
    return a list of n cards
    """
    random.shuffle(cards_list)
    return [cards_list.pop() for k in range(n)]

# new deck
cards_list = new_deck()
print("New deck = %s cards" % len(cards_list))  # test
# draw n cards per hand
n = 5
# draw the hands
hand1 = draw_cards(n, cards_list)
hand2 = draw_cards(n, cards_list)

print('-'*40)
# show the 2 hands
print("hand1 = %s" % hand1)
print("hand2 = %s" % hand2)
print('-'*40)

print("New deck = %s cards" % len(cards_list))  # test

"""my random result -->
New deck = 52 cards
----------------------------------------
hand1 = ['4C', 'JH', '9H', 'TC', '4H']
hand2 = ['TH', 'KC', '5D', '6D', 'KD']
----------------------------------------
New deck = 42 cards
"""

I'm using Kristopherr's structure:

import random
creating the deck of cards
cards = ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S',\
         'AD', 'KD', 'QD', 'JD', '10D', '9D', '8D', '7D', '6D', '5D', '4D', '3D', '2D',\
         'AC', 'KC', 'QC', 'JC', '10C', '9C', '8C', '7C', '6C', '5C', '4C', '3C', '2C',\
         'AH', 'KH', 'QH', 'JH', '10H', '9H', '8H', '7H', '6H', '5H', '4H', '3H', '2H']


player = input('How many players? ')
hand1 = []
hand2 = []
hand3 = []
hand4 = []
hand5 = []
hand6 = []
hand7 = []
hand8 = []
hand9 = []
hand10 = []
hand11 = []
hand12 = []
hand13 = []
hand14 = []
hand15 = []

#shuffle the cards
random.shuffle(cards)
num = input('How many cards to deal to each player? ')
while num > 0:
    if player == 1:
        hand1.append(cards.pop(0))
    if player == 2:   
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
    if player == 3: 
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
    if player == 4: 
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
    if player == 5: 
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
    if player == 6:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
    if player == 7:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
    if player == 8:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
    if player == 9:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
        hand9.append(cards.pop(0))
    if player == 10:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
        hand9.append(cards.pop(0))
        hand10.append(cards.pop(0))
    if player == 11:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
        hand9.append(cards.pop(0))
        hand10.append(cards.pop(0))
        hand11.append(cards.pop(0))
    if player == 12:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
        hand9.append(cards.pop(0))
        hand10.append(cards.pop(0))
        hand11.append(cards.pop(0))
        hand12.append(cards.pop(0))
    if player == 13:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
        hand9.append(cards.pop(0))
        hand10.append(cards.pop(0))
        hand11.append(cards.pop(0))
        hand12.append(cards.pop(0))
        hand13.append(cards.pop(0))
    if player == 14:
        hand1.append(cards.pop(0))
        hand2.append(cards.pop(0))
        hand3.append(cards.pop(0))
        hand4.append(cards.pop(0))
        hand5.append(cards.pop(0))
        hand6.append(cards.pop(0))
        hand7.append(cards.pop(0))
        hand8.append(cards.pop(0))
        hand9.append(cards.pop(0))
        hand10.append(cards.pop(0))
        hand11.append(cards.pop(0))
        hand12.append(cards.pop(0))
        hand13.append(cards.pop(0))
        hand14.append(cards.pop(0))
    num = num - 1
if player == 1:
    print hand1
if player == 2:
    print hand1
    print hand2
if player == 3:
    print hand1
    print hand2
    print hand3
if player == 4: 
    print hand1
    print hand2
    print hand3
    print hand4
if player == 5:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
if player == 6:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
if player == 7:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
if player == 8:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
if player == 9:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
    print hand9
if player == 10:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
    print hand9
    print hand10
if player == 11:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
    print hand9
    print hand10
    print hand11
if player == 12:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
    print hand9
    print hand10
    print hand11
    print hand12
if player == 13:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
    print hand9
    print hand10
    print hand11
    print hand12
    print hand13
if player == 14:
    print hand1
    print hand2
    print hand3
    print hand4
    print hand5
    print hand6
    print hand7
    print hand8
    print hand9
    print hand10
    print hand11
    print hand12
    print hand13
    print hand14

print 'the cards remaining in the deck are: '
print cards

I'm trying to make it draw a card while someone has a hand of any number cards, for instance 5. I want to make it ask "How many cards do you want" i'll put in a number, and it will give me the respective cards.

BTW this ISN'T homework

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.