开发者

How to use for loop to print cards?

I have all 52 cards set up, and i try to print all 52 cards by using for loop. I don't know how to set my for loop at this point.

def define_cards(n):
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    cards = []
    for suit in range(4):
        for rank in range(13):
            card_string = rank_string[rank] + " of " + suit_string[suit]
            cards.append(card_string)

print "The cards are:"
for i in range(52):  开发者_高级运维            #how to make this for loop work??
    print i, card_string[i]

I want to print like this

The crads are:
0 ace of clubs
1 two of clubs
2 three of clubs
...
49 jack of spades
50 queen of spades
51 king of spades


Your function define_cards has to return the list. Add return cards at its end.

Then you have to actually call/execute this function.

Then you can access individual cards in this list:

cards = define_cards()
for i, card in enumerate(cards):
    print i, card

However, if you are searching for a "more pythonic" solution, try this:

import itertools as it

rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
suit_string = ("clubs","diamonds","hearts","spades")

print 'The cards are:'
for i, card in enumerate(it.product(rank_string, suit_string)):
    print i, '{0[1]} of {0[0]}'.format(card)


Look at just this

    cards.append(card_string)

print "The cards are:"
for i in range(52):              #how to make this for loop work??
    print i, card_string[i]

Why print card_string[i]?

What's wrong with cards[i]?


ranks = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
suits = ("clubs","diamonds","hearts","spades")

Answer is an elegant one-liner:

cards = [rank+' of '+suit for suit in suits for rank in ranks]

for i,card in enumerate(cards):
    print i, card

Result:

0 ace of clubs
1 two of clubs
...
50 queen of spades
51 king of spades


def define_cards():
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    cards = []
    n = 0
    for suit in suit_string:
        for rank in rank_string:
            print '%s %s of %s' % (n,rank,suit)
            n+=1

define_cards()


Why not use a iterator :

def define_cards():
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    for suit in suit_string:      # you can obtain the items of the iterate list directly, no need of rank access
        for rank in rank_string:
            card_string = rank + " of " + suit
            yield card_string

print "The cards are:"
cards_iterator = define_cards()
for i, card in enumerate(cards_iterator):   # use the iterator power ;)
    print i, card
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜