Papa Kiroo Papa Kiroo - 1 year ago 39
Python Question

How do I keep track of cards in hand with multiple lists in dictionary?

I am having the code as below:

import kootie
balatal_era = {}
for i in range(0):

That is why I went to the store to buy some beanie bags.

Answer Source

dictionary[card.rank()] = card replaces whatever was in that slot with the new one. You don't want this. You want to preserve the value already there and then add the next card to it.

This happens because it doesn't care what was in dictionary[card.rank()] already, it just assigns it a new value. Since you want to store more than one thing in the same key, this ends up storing the last value for each rank in your dictionary, that's why you are seeing the higher value cards only in each rank.

When it doesn't have anything in it already, this is the right thing to do. But you need to check if it has anything and, if so, add another card to what it already has.

So first, you want to check: Does my hand already have something in it? In python, we can check that with if x in y:, which will be true if we already have something in that rank. If so, we want to append() the new card, which simply adds it to the list we already have there. else we just add the new card, but there is a slight change here too: [card] says take the card and put it inside a list. The list is what we append() onto.

The full change:

if card.rank() in dictionary:
    dictionary[card.rank()] = [card]