为什么我不能从字典中追加这个值?

时间:2015-12-18 01:43:39

标签: python arrays python-3.x dictionary

我有一个程序,目的是模拟纸牌游戏21.以下是我的代码的重要元素,它们非常自我解释(我已经突出显示了我稍后会提到的行)

spades = ['2S','3S','4S','5S','6S','7S','8S','9S','10S','JS','QS','KS','AS']
hearts = ['2H','3H','4H','5H','6H','7H','8H','9H','10H','JH','QH','KH','AH']
clubs = ['2C','3C','4C','5C','6C','7C','8C','9C','10C','JC','QC','KC','AC']
diamonds = ['2D','3D','4D','5D','6D','7D','8D','9D','10D','JD','QD','KD','AD']
allCards = spades + hearts + clubs + diamonds

cardVal = {'2S':2,'3S':3,'4S':4,'5S': 5,'6S':6,'7S':7,'8S':8,'9S':9,'10S':10,'JS':10,'QS':10,'KS':10,'AS':11,
    '2H':2,'3H':3,'4H':4,'5H':5,'6H':6,'7H':7,'8H':8,'9H':9,'10H':10,'JH':10,'QH':10,'KH':10,'AH':11,
    '2C':2,'3C':3,'4C':4,'5C':5,'6C':6,'7C':7,'8C':8,'9C':9,'10C':10,'JC':10,'QC':10,'KC':10,'AC':11,
    '2D':2,'3D':3,'4D':4,'5D':5,'6D':6,'7D':7,'8D':8,'9D':9,'10D':10,'JD':10,'QD':10,'KD':10,'AD':11}

import random
random.shuffle(allCards)

playerCards = [allCards.pop() for i in range(2)]
dealerCards = [allCards.pop() for i in range(2)]
playerHand = []
dealerHand = []
playerHandVal = 0
dealerHandVal = 0

def handVal(playercards,playerhand,score):
    playerhand = []
    for i in playercards:
        playerhand.append(cardVal[i]) ####### LINE 29 ######
    score = sum(playerhand)
    print(score)

handVal(playerCards,playerHand,playerHandVal)
handVal(dealerCards,dealerHand,dealerHandVal)

def twist(playercards,playerhand,score):
    newCard = [allCards.pop() for i in range(1)]
    playercards.append(newCard)
    handVal(playercards,playerhand,score)    ####### LINE 39 ########

move = input('Stick (S) or Twist (T) : ')

if move == 'T' or move == 't':
    while move == 'T' or 't':
        twist(playerCards,playerHand,playerHandVal)  ######## LINE 45 ########
        print(playerHand)
        if playerHandVal > 21:
            move = 's'
            break
        move = input('Stick (S) or Twist (T) : ')

当我运行脚本时,它将生成两个长度为2的数组,并在输入提示符Stick (S) or Twist (T) :之前为这些卡提供正确的手值。但是,当选择扭曲时,程序会产生此错误,

  line 45, in <module>
    twist(playerCards,playerHand,playerHandVal)
  line 39, in twist
    handVal(playercards,playerhand,score)
  line 29, in handVal
    playerhand.append(cardVal[i])
TypeError: unhashable type: 'list'

我的问题是,为什么代码playerhand.append(cardVal[i])的这一部分最初起作用,但在转义函数中调用时却不起作用

2 个答案:

答案 0 :(得分:5)

当你这样做时:

newCard = [allCards.pop() for i in range(1)]
playercards.append(newCard)

您在list中插入一个playercards,其中包含从allCards弹出的单个值,而不是值本身。

稍后,当你这样做时:

for i in playercards:
    playerhand.append(cardVal[i]) ####### LINE 29 ######

i不是索引,也不是键本身,它是一个元素list,包含我认为是你想要的键; list是可变的,因此不适合作为dict的密钥,cardValdict

最初有效,因为您将playerhand初始化为包含两张卡片的list,而不是list单张list张卡片playerCards = [allCards.pop() for i in range(2)] 1}},这是你错误实现的额外绘图。

修复方法是使用卡片填充playercards,而不是使用单元素list卡片填充:

newCard = allCards.pop() # Get the value, not a one-element `list` containing the value
playercards.append(newCard)

如果目标是弹出并附加多张卡片(并且暂时只使用range(1)作为占位符),则单独附加每张卡片,而不是附加list张卡片作为单个元素,您可以执行以下任一操作:

newCard = [allCards.pop() for i in range(1)]
playercards.extend(newCard)  # extend appends each element from the iterable

或者playercardsnewCard都是list,您可以使用运算符重载:

newCard = [allCards.pop() for i in range(1)]
playercards += newCard  # Same as extend, but only works when both sides are same type

答案 1 :(得分:1)

因为列表推导[allCards.pop() for i in range(1)]的返回值本身就是一个列表。所以在这些方面之后:

newCard = [allCards.pop() for i in range(1)]
playercards.append(newCard)

playercards可能包含['KS', 'QH', ['JS', '10H']]的内容。

稍后在handVal中,您尝试使用列表中的值索引dict(cardVal)。您无法使用可变对象类型索引dict,因此您将收到此错误。

您可能希望将第38行更改为:

playercards.extend(newCard)