Blackjack:如何构建一个函数来查找每只手的最大值并在Python 3.x中返回该值

时间:2018-02-02 06:32:37

标签: python python-3.x

所以我写了一个由多个功能组成的二十一点程序。我在编写一个特定函数的代码时遇到了困难,它找到了所有指针的最大值(由用户输入)并返回它。这是我到目前为止所尝试的内容:

CONNECT failed: RefusedNotAuthorized

我要添加以前与该主题相关的功能,以便进行更好的测量:

cards_value = {"A":1, "T":10, "J": 10, "K":10, "Q": 10 }
def get_total(card):
    return cards_value[card[1]]

def get_max(hands): 
        hand_value = 0
        ace = False
        for card in hands:
            if card[0] == "A":
                ace =True;
            if card[:-1] in cards_value:  
                 hand_value += cards_value[card[:-1]]
            else:
                 hand_value += int(card[:-1])
        if ace and hand_value + 10 < 22:
                 hand_value += 10
        return hand_value

编辑 - 输出应如下所示:

deck = making_deck() # a function that shuffles 52 cards and makes deck.
def bjack_deal(deck, num_players): # num_players is a parameter manipulated by user

    hands = []
    for i in range(numplayers):

        hands.append([deck.pop(), deck.pop()])

    return hands

1 个答案:

答案 0 :(得分:0)

我不确定你想要什么,但这可能会有所帮助:

>>> cards_value = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10,'K':10}
>>> hands = [['4D', 'KD'], ['QS', '9C'], ['TC', 'QC']]
>>> sums = [sum(map(lambda x: cards_value[x[0]], hand)) for hand in hands]
>>> sums
[14, 19, 20]
>>> max(sums)
20