2011-05-05 85 views
1

我已经设置了所有52张卡,并且我尝试使用for loop打印所有52张卡。 我不知道如何设置我的for loop在这一点上。如何使用循环打印卡片?

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] 

我想打印这样

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 
+0

这是一个班轮:'卡=枚举(秩+ '的' +西装套装中的军装)' - 详情请参阅回答 – ninjagecko 2011-05-05 09:57:11

+0

请用[家庭作业]标记标记作业。 – 2011-05-05 10:02:30

回答

4

你的功能define_cards必须返回列表中。在末尾添加return cards

然后你必须实际调用/执行这个函数。

然后你就可以访问各个卡在这个名单:

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

不过,如果你正在寻找一个“更Python”的解决方案,试试这个:

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) 
+0

我已经尝试你的第一个答案,是工作,但为什么只打印一次? – phhnk 2011-05-05 10:27:28

+0

@phhnk - 你在哪里放了'回卡'?在函数结尾处,在'范围(4):'的范围之下,所以缩进4个空格? – eumiro 2011-05-05 11:01:14

+0

我被放在下面'cards.append(card_string)' – phhnk 2011-05-05 11:10:56

1

为什么不使用迭代器:

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

我仍然是一个noob,仍然有学习... – phhnk 2011-05-05 10:28:33

2

看看眼前这个

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] 

为什么要打印card_string[i]

cards[i]怎么了?

+0

我已经尝试过'卡片式[i]',但它不起作用。 – phhnk 2011-05-05 10:04:28

+1

@phhnk:“不起作用”是什么意思?我无法猜测。额外感官知觉我没有得到祝福。 – 2011-05-05 10:07:05

+0

@phhnk - 只有在执行你的'define_cards'函数后才能工作。 – eumiro 2011-05-05 10:07:53

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

回答是优雅的单行:

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

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

结果:

0 ace of clubs 
1 two of clubs 
... 
50 queen of spades 
51 king of spades 
1
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()