扑克手串显示

时间:2010-11-13 00:42:05

标签: python string list search

我是编程和Python的新手,我正努力去理解和学习它。我不是要求答案,而是用简单的非计算机术语解释,以便我可以尝试自己制定解决方案。

这是我遇到的另一个问题。我有以下4个列表:

short_card = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
long_card = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 'five', 'four', 'three', 'deuce']
short_suit = ['c', 'd', 'h', 's']
long_suit = ['clubs', 'diamonds', 'hearts', 'spades']

现在应该做的是写两个函数:card_str(c)和hand_str(h)。

card_str(c)采用两个字符的字符串并搜索以找出相应的字符以在文本中显示卡片。例如,如果我把'kh'程序输出“心之王”。

hand_str(h)获取两个字符串的列表,并以全文显示相应的手。再举个例如,如果我把([“Kh”,“As”,“5d”,“2c”]),它将输出“心之王,黑桃王牌,钻石之五,俱乐部之谜”。

以下是我到目前为止:

short_card = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
long_card = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six',      'five', 'four', 'three', 'deuce']
short_suit = ['c', 'd', 'h', 's']
long_suit = ['clubs', 'diamonds', 'hearts', 'spades']

def card_str(c):

def hand_str(h):


#- test harness: do not modify -# 

for i in range(13):  
print card_str(short_card[i] + short_suit[i%4])

l = []
for i in range(52):
    l.append(short_card[i%13] + short_suit[i/13])
print hand_str(l)

4 个答案:

答案 0 :(得分:1)

所以你拥有的是两组列表,它们将输入值与输出字符串相关联。注意列表的顺序;他们是一样的。哪个应该使输入的索引值等于...

答案 1 :(得分:1)

你没有太多,但我会说你的名单是成对的。

short_card = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
long_card = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 'five', 'four', 'three', 'deuce']

short_suit = ['c', 'd', 'h', 's']
long_suit = ['clubs', 'diamonds', 'hearts', 'spades'] 

它们各自的长度和顺序相同。所以short_card中'A'的索引与long_card中'ace'的索引相同。因此,如果您找到索引为1,则您拥有另一个索引。

这应该指向正确的方向。当你有更多时,请回来编辑你的帖子。

答案 2 :(得分:0)

我对最后两张海报略有不同,并以zip功能开始加入匹配列表。

答案 3 :(得分:0)

>>> def poker_card(c):
...     card, suit = c
...     short_cards = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
...     short_suits = ['c', 'd', 'h', 's']
...     long_cards = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six',      'five', 'four', 'three', 'deuce']
...     long_suits = ['clubs', 'diamonds', 'hearts', 'spades']
...     return "%s of %s" % (long_cards[short_cards.index(card)], long_suits[short_suits.index(suit)])
... 
>>> def poker_hand(hand):
...     return [poker_card(c) for c in hand]
... 
>>> def main():
...     print poker_hand(["Kh", "As", "5d", "2c"])
... 
>>> main()
['king of hearts', 'ace of spades', 'five of diamonds', 'deuce of clubs']
相关问题