I am currently learning Python and how to work with start and stop indexing. I am doing a project from the book I am learning from and I'm stumped...I need to display a full deck of cards, with

numbers = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
suits = ("c", "h", "s", "d")

so that it displays the entire deck, paired and ordered to look like:

Ac Ah As Ad
2c 2h 2s 2d
3c 3h 3s 3d
.
.
.
.
Qc Qh Qs Qd
Kc Kh Ks Kd

so far all I have are the numbers and suits set up but I keep trying to do things way too complicated with for loops, so i completely deleted my code and came here for fresh ideas.

Thanks

One way is like this
Take numbers and add like this "A" count as 1.

>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + 'Jack Queen King'.split()
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
>>> 
>>> suits = ['diamonds', 'clubs', 'hearts', 'spades']
>>> suits
['diamonds', 'clubs', 'hearts', 'spades']

>>> deck = ['%s of %s' % (n, s) for n in numbers for s in suits]
>>> deck
# the hole deck

And we can make it look better.
>>> from pprint import pprint
>>> pprint(deck[:10])  #52 gives the hole deck
['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']
>>>

I am not sure which version of Python you are using, but if you explore this code ...

rank = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
suit = ("c", "h", "s", "d")

deck = []
for r in rank:
    for s in suit:
        card = r + s
        deck.append(card)

print( deck )

... you can see the the list called deck is almost ordered the way you want it. All you need to do now is to print four of the list's elements in a row and then slip in a newline character. Put your thinking hat on!

Just to explain one thing look at vegaseat code.
Does it look like this line?

deck = ['%s of %s' % (n, s) for n in numbers for s in suits]

This is called list List Comprehensions
I can wite it like this.

deck = []
for n in numbers:
    for s in suits:
        card = '%s of %s' % (s, n)
        deck.append(card)

Then you see it not so differnt.

This article has been dead for over six months. Start a new discussion instead.