简体   繁体   English

为什么不能从字典中追加此值?

[英]why can't I append this value from a dictionary?

I have a program with the purpose of emulating the card game 21. Here are the important elements to my code which are pretty self explanitory (I have highlighted lines i will mention later) 我有一个旨在模拟纸牌游戏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) : ')

When I run the script it will produce two arrays of length 2 and give the correct hand value for these cards before the input prompt Stick (S) or Twist (T) : . 当我运行脚本时,它将产生两个长度为2的数组,并在输入提示Stick (S) or Twist (T) :之前为这些卡提供正确的手牌值Stick (S) or Twist (T) : However, when twist is chosen the program produces this error, 但是,当选择扭曲时,程序会产生此错误,

  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'

My question is, why does this part of the code playerhand.append(cardVal[i]) work initally but not when called in the twist function 我的问题是,为什么代码playerhand.append(cardVal[i])这一部分playerhand.append(cardVal[i])工作,但在twist函数中调用时却不能工作

When you do: 当您这样做时:

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

you're inserting a list into playercards containing a single value popped from allCards , not the value itself. 您正在将一个list插入到playercards其中包含从allCards弹出的单个值,而不是值本身。

Later, when you do: 稍后,当您这样做时:

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

i is not an index, or the key itself, it's a one element list containing what I assume is the key you want; i不是索引,也不是密钥本身,它是一个元素list其中包含我认为是您想要的密钥; list s are mutable, and therefore not suitable as keys for dict s, and cardVal is a dict . list是可变的,因此不适合用作dict的键,而cardValdict

It worked initially, because you initialize playerhand as a list containing two cards, not a list of single-element list s of cards when you do playerCards = [allCards.pop() for i in range(2)] , it's the additional draws that you implement incorrectly. 它的工作开始,因为你初始化playerhand作为list包含两个卡,而不是一个list的单元素list的卡那时你做playerCards = [allCards.pop() for i in range(2)] ,它的附加彩票您实施不正确。

The fix is to populate playercards with cards, not one-element list s of cards: 解决方法是用纸牌而非单元素list填充playercards纸牌:

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

If the goal was to pop and append several cards (and you were just using range(1) as a placeholder for the time being), to append each card individually, rather than appending the list of cards as a single element, you could do either: 如果目标是弹出并附加几张卡(并且您暂时仅使用range(1)作为占位符),则要分别添加每张卡,而不是将卡list作为单个元素附加,要么:

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

or since playercards and newCard are both list s, you can use the operator overload: 或者因为playercardsnewCard都是list ,所以可以使用运算符重载:

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

Because the return value from the list comprehension [allCards.pop() for i in range(1)] is a list itself. 因为列表[allCards.pop() for i in range(1)]的返回值[allCards.pop() for i in range(1)]是列表本身。 So after these lines: 因此,在这些行之后:

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

playercards might hold something that looks like ['KS', 'QH', ['JS', '10H']] . playercards可能包含类似['KS', 'QH', ['JS', '10H']]

Later in handVal you are trying to index a dict ( cardVal ) with the values in a list. 稍后在handVal您尝试使用列表中的值索引字典( cardVal )。 You can't index a dict with a mutable object type so you're getting this error. 您无法为具有可变对象类型的字典编制索引,因此会出现此错误。

You probably want to change line 38 to something like: 您可能希望将第38行更改为以下内容:

playercards.extend(newCard)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM