Hey guys. I just started writing a blackjack game and it wasn't long at all before I ran into the mutability issue that I'm having. I thought I had it all figured out already before I started the project. But apparently not. Heres what I got so far. I created a deck of cards with a dictionary. The keys are the 4 suits. And the values for each key are obviously the 13 card faces of the cards. I called this "full_deck". I then made a copy called "available_cards" so that I can remove cards from it without affecting the "full_deck" reference. But when I remove cards from the "available_cards" it does in fact also remove from the "full_deck". I thought the way around this was to use the .copy() method instead of just an = assignment but now I see thats not true. I asked chat-gpt and it told me that the issue is that the .copy() method only does a shallow copy and in order to do a deep copy it recommended importing the "copy" module. Which is cool. That seams like a fine way to go. But I just want to make sure with you guys. Is that really the best way to go? Do you need to import a module or is there another way? Heres a cut down version of what Im working with..
from random import randint
card_numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = ["hearts", "clubs", "spades", "diamonds"]
full_deck = {}
for i in range(len(suits)):
full_deck[suits] = [x for x in card_numbers]
available_cards = full_deck.copy()
while True:
suit = randint(0, 3)
card = randint(0, 12)
available_cards[suits[suit]].pop(card)
print(f'{card_numbers[card]} of {suits[suit]}')
print('Full Deck:')
print('\n'.join([f'{k}: {v}' for k, v in full_deck.items()]))
print()
print('Available Cards:')
print('\n'.join([f'{k}: {v}' for k, v in available_cards.items()]))
print()
print(f'Card picked: {card_numbers[card]} of {suits[suit]}')
ans = input('Press ENTER or (x) to quit:')
if ans.lower() == 'x':
break
from random import randint
card_numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = ["hearts", "clubs", "spades", "diamonds"]
full_deck = {}
for i in range(len(suits)):
full_deck[suits] = [x for x in card_numbers]
available_cards = full_deck.copy()
while True:
suit = randint(0, 3)
card = randint(0, 12)
available_cards[suits[suit]].pop(card)
print(f'{card_numbers[card]} of {suits[suit]}')
print('Full Deck:')
print('\n'.join([f'{k}: {v}' for k, v in full_deck.items()]))
print()
print('Available Cards:')
print('\n'.join([f'{k}: {v}' for k, v in available_cards.items()]))
print()
print(f'Card picked: {card_numbers[card]} of {suits[suit]}')
ans = input('Press ENTER or (x) to quit:')
if ans.lower() == 'x':
break