2011-05-05 86 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()