如何编写代表GUI卡的字符串?

时间:2015-04-22 20:33:27

标签: python user-interface

我正在用Python编写一个纸牌游戏程序,我希望GUI能够显示所有牌。

suits = ['spades','hearts','diamonds','clubs']
ranks = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']

def create_deck():
    deck = []
    for suit in suits:
        for rank in ranks:
            deck.append((suit,rank))
    return deck

def shuffle(deck):
    for i in range(100):
        card = deck.pop(randint(0,51)) 
        deck.append(card)

values = {}
i = 1
for card in deck:
    values[card]=i
    if i == 13:
        i = 1
    else:
        i += 1
print values #dictionary mapping values to cards

我想要一个函数来写一个表示手中每张卡的字符串,我相信可以通过迭代一个函数来完成,该函数将代表每张卡的字符串写在手中的每张卡上。但是,我不确定它应该是什么样的(例如,如果我需要函数的参数,如果参数应该是'suit,rank',或者只是'card'等)。

1 个答案:

答案 0 :(得分:1)

>>> my_hand = [('spades','ace'), ('hearts','five'), ('hearts','six')]
>>> def showhand(hand):
...     return '\n'.join([rank.title() + ' of ' + suit.title() for (suit,rank) in hand])
...
>>> print(showhand(my_hand))
Ace of Spades
Five of Hearts
Six of Hearts