当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《为什么我不能从字典中追加这个值?》经验,为你挑选了1个好方法。

我有一个程序,目的是模拟纸牌游戏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 
    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])最初工作,而不是在twist函数中调用时



1> ShadowRanger..:

当你这样做时:

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

你插入listplayercards含有从弹出一个值allCards,而不是价值本身.

以后,当你这样做时:

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

i不是索引,也不是键本身,它是一个list包含我认为是你想要的键的元素; lists是可变的,因此不适合作为dicts的键,并且cardVal是a dict.

它最初起作用,因为你初始化playerhandlist包含两张卡,而不是list单元素list的卡片playerCards = [allCards.pop() for i in range(2)],这是你不正确实现的额外绘图.

修复是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

或者既然playercardsnewCardlists,你可以使用运算符重载:

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

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有