I'm having a little trouble with a piece of code im trying to write.

I have created a deck of cards and then shuffled them however now I am trying to covert my deck (which is simply a list containing the numbers between 1 and 52) into the form "AS", "9C" etc.

Im relatively new to python therefore if anyone can give me a hint in the right direction that would be great thanks.

Member Avatar for masterofpuppets

hi,
here's a hint :)

you have four suits [ "S", "H", "D", "C" ]
you can loop through the list of cards, i.e. [ "1", "2", "3", ..."52" ] and for the first 13 cards add the "S" suit to the card number to get "1S"....and so on for all cards up to 13, when you get to 13 change the suit.

hope this gives you some idea. Let me know if you need some more help. :)

If you keep track of the sort (or shuffle) order, you can just refer to that position in a fixed array.

arr_strCards = [
	"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",
	"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
	"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
	"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD"
	]

What I am able to do so far is change the suit to each card correctly for example "1S", "52C". However the bit im having trouble with is changing the number in front.

I am trying to do this using loops and if statements going through each index of the shuffled deck of cards.

>>> suits = ['diamonds', 'clubs', 'hearts', 'spades']
>>> rank = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
>>> # This is what we need to make a hole deck
>>> # Now we loop trough (rank and suits) and append the result.
>>> deck = []  # Empty deck
>>> for n in numbers:
	for s in suits:
		card = '%s of %s' % (n, s)
		deck.append(card)
		
>>> deck  # Will print the whole deck

>>> # Make it look better
>>> from pprint import pprint
>>> # Now let us print 10 card
>>> pprint(deck[:10])
['1 of diamonds',
 '1 of clubs',
 '1 of hearts',
 '1 of spades',
 '2 of diamonds',
 '2 of clubs',
 '2 of hearts',
 '2 of spades',
 '3 of diamonds',
 '3 of clubs']
>>> # The for loop can be written in 1 line like this
>>> deck = ['%s of %s' % (n, s) for n in numbers for s in suits]
>>> # A little python sugar with list comprehension

Now you can to shuffle the deck,use random module.
Deck is a list now,so to take out 1 card should be easy,look at pop() method.
To se all method for list do this dir(deck)

You want to keep your card symbols simple and unique to be able to evaluate the hand ...

suit_str = "CDHS"
rank_str = "23456789TJQKA"

deck = [rank+suit for suit in suit_str for rank in rank_str]

print(deck)

"""my output (made pretty) -->
[
'2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC', 'AC',
'2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD', 'AD', 
'2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH', 'AH', 
'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS', 'AS'
]
"""

# now if you want to deal a hand of 5 cards do it this way
import random

random.shuffle(deck)
# pick five cards
hand = deck[:5]
# show the hand
print(hand)     # for instance ['JH', '9C', 'QS', '6D', 'QD']

If you want to expand the somewhat cryptic card symbols for the user, write a small function that turns "JH" into "Jack of Hearts" and so on.

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.