Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to list every card in a deck of cards (along with a number assigned to the card) using this code:

suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")

def translate():
    rank = ""
    suit = ""
    cards = ""
    cardNum = 0

    for x in rankName:
        rank = x

    for y in suitName:
        suit = y

    for i in range(0, NUMCARDS):
        cards += rank
        cards += " of "
        cards += suit
        cardNum = i
        i += 1

        print cardNum
        print "    "
        print cards

My output is only getting "King of clubs" 52 times though. What do I need to do?

share|improve this question
    
Ah, the joys of indentation levels. –  Ryaminal Jun 14 '13 at 23:05

2 Answers 2

up vote 2 down vote accepted

Your loops should be nested, but right now they just execute in order. Right now, rank goes through all rankNames and gets set to the last one, suit goes through all suitNames and gets set to the last one, and then they're printed out 52 times.

You shouldn't even really have that last loop.

suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")

def translate():
    cardNum = 0

    for x in rankName:
        rank = x

        for y in suitName:
            suit = y

            cards = ""
            cards += rank
            cards += " of "
            cards += suit
            i += 1

            print cardNum
            print "    "
            print cards

Also, rank = x and suit = y could just be for rank in rankName: and for suit in suitName. Also also, pre-initializing the variables isn't really useful.

share|improve this answer
    
This code here is giving me errors because of the i reference... –  Rachelle Bennington Jun 14 '13 at 23:04
    
Never mind, I figure it out. Set the cardNum to += 1 instead. –  Rachelle Bennington Jun 14 '13 at 23:10

There are built-in methods for accomplishing what you want as well.

import itertools

suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")

cross_product = itertools.product(rankName, suitName)

for card_num, (rank, suit) in enumerate(cross_product, start=1):
  print("{0}    {1} of {2}".format(card_num, rank, suit))
share|improve this answer
    
im afraid that this is not cleaner than two simple for loops, quite the opposite is the case IMO –  Paranaix Jun 14 '13 at 23:22
1  
@Paranix intertools.product and enumerate are encapsulated, generic versions of the for loops. You should read up on them, as they much easier to understand if you know them. –  Swiss Jun 14 '13 at 23:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.