显示列表python3中值的文本

时间:2014-12-16 07:52:01

标签: list python-3.x blackjack

我有2个名单:

deck=[2,2,2,2,3,3,3,3,4,4,4,4,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11]

symbols=['\u2660', '\u2661', '\u2662', '\u2663','\u2660', '\u2661', '\u2662', '\u2663','\u2660',          '\u2661', '\u2662', '\u2663','\u2660', '\u2661', '\u2662', '\u2663','\u2660', '\u2661', '\u2662', '\u2663','\u2660', '\u2661', '\u2662', '\u2663','\u2660', '\u2661', '\u2662', '\u2663','\u2660', '\u2661', '\u2662', '\u2663']

和一个功能

def dealCard(deck,participant):#deal cards , chooses between player and house with string z
  participant.append(deck.pop())
  if z==1:
      print('\nYou got  %s ' %("{} {}".format(player[len(player)-1],symbols.pop()))) 
  else:
      print('\nHouse got %s ' %("{} {}".format(house[len(house)-1],symbols.pop())))`

有没有办法显示字母' A' (代表王牌)代替11?

例如

>>>You got A ♡ 

而不是

>>>You got 11 ♡ 

1 个答案:

答案 0 :(得分:0)

使用数字1..13表示卡片等级(A,2,3,...,Q,K)。然后使用数字0..3来表示套装。然后用这两个值制作一个卡片对象,或者用数学方法将它们组成一个int(我个人喜欢4 * rank + suit)。使用对象看起来像这样:

def Card(object):
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return ["A","2","3","4","5","6","7","8","9","10","J","Q","K"][self.rank-1] +
          ['\u2660', '\u2661', '\u2662', '\u2663'][suit]

使用 str 功能,您将能够使用打印等。将卡组构建为卡对象列表:

deck = []
for rank in range(13):
    for suit in range(4):
        deck.append(Card(rank+1, suit))
random.shuffle(deck)

手也只是一张牌列表。一手牌就像这样:

def total(hand):
    acefound, soft = False, False
    total = o

    for c in hand:
        if c.rank > 10:
            total += 10
        else:
            total += c.rank
        if c.rank == 1:
            acefound = True

    if acefound and total < 12:
        total += 10
        soft = true

    return total, soft