Reputation: 3
I am working on a blackjack game and need to collect the score of all the cards the player has. When i use the following code I get unhashable type: 'list' error.
Cards is a dictionary that holds all the cards and their respective blackjack points score and p a dictionary which holds the players cards (as they would be keys in the cards dictionary.
score = cards[list(p.values())]
Any help on how to do this would be greatly appreciated.
Upvotes: 0
Views: 237
Reputation: 2139
You got that error just because of you are trying to use list
as dictionary keys, which is prohibited in Python. [to know why look at @SyntaxError answer]
try following code instead of yours
score = sum(cards[value_of_p] for value_of_p in p.values())
Upvotes: 1
Reputation: 5677
That error means that lists in python can't be used as dictionary keys.
What you want is to access the dictionary values for each element of that list, so try to use a list comprehension.
score = sum(cards[c] for c in p.values())
Upvotes: 2
Reputation: 370
A list can never be the key of a dictionary.
Why Lists Can't Be Dictionary Keys
Upvotes: 1