扑克手牌字符串显示
我刚开始接触编程和Python,正在努力学习和理解这些内容。我并不是在寻求答案,而是希望能用简单易懂的语言来解释,这样我才能自己尝试找出解决方案。
我遇到了一个问题。下面有四个列表:
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
我会稍微不同于前面两位的做法,首先使用 zip
函数来连接匹配的列表。
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'的索引是相同的。因此,如果你找到了一个的索引,你也就找到了另一个的索引。
这应该能给你一些提示。等你有更多信息时,记得回来编辑你的帖子。