Changing variables as dictionary values

I am making a poker game and I ran into something I don’t quite understand about dictionaries when trying to make a scoreboard variable. The scoreboard variable is a dictionary where they keys are instances of a player class. Each player is initialized with a score attribute that is set by default to zero. I use the following code to calculate the score based upon how many cards each player has left in their hand and then add it to their score:

score_board = {player1: player1.score, player2: player2.score, player3: player3.score, player4: player4.score}

for player in players:
    if len(player.hand) == 0:
        for i in players:
            if len(i.hand) <= 7:
                player.score += len(i.hand)
            elif len(i.hand) > 7 and len(i.hand) <= 13:
                player.score -= 2 * len(i.hand)
            elif len(i.hand) == 13:
                player.score += 3 * len(i.hand)
    elif len(player.hand) <= 7:
        player.score -= len(player.hand)
    elif len(player.hand) > 7 and len(player.hand) <= 13:
        player.score -= 2 * len(player.hand)
    elif len(player.hand) == 13:
        player.score += 3 * len(player.hand)


This code prints:
{player1: 0, player2: 0, player3: 0, player4: 0}

I know that the players scores are updating, it’s just not reflected in the dictionary. Can someone please explain why?

Your dictionary values are not referring to the player1.score attribute itself. They’d refer to whatever object it referenced at the time they were created (an immutable integer value). Updating player1.score for example changes what player1.score points to but it cannot change the score_board dictionary value.

For a short example of this behaviour-

val = 3
dct = {'a': val}
val = 5
print(dct)  # prints... {'a': 3}

In this code you are not showing any change to dictionary values. The dictionary can’t keep track of data without being updated (it’s not a pointer to outside data).

There’s also the issue of the data types in your dictionary. It looks like you don’t have string keys, rather… object-type keys? In any case you would need an operation like score_board[player1] += #whatever point modification. But it’s maybe more common to see keys as strings score_board[] += #whatever point modification