i'm working now on a BlackJack program, and i want to make a full tail of cards in one dictionary, like..
cards = {
"2S" : 2,
"2H" : 2,
"2D" : 2,
"2C" : 2,
"3S" : 3,
"3H" : 3,
"3D" : 3,
"3C" : 3
.
.
.
}
... and then add random cards to other dictionaries:
player_cards = {}
computer_cards = {}
How can i move random items from cards
dictionary to other dictionary?
CodePudding user response:
import random
random_card = random.choice(list(cards))
player_cards[random_card] = cards[random_card]
CodePudding user response:
Please clarify your question
I assume the values in the dictionary "cards" are the number of cards remaining for each face and value, but that does not make much sense since you have multiple 2 of spades (I suppose this means you use multiple decks? But that would mean it is possible to draw two 2 of spades consecutively).
If this is what you want, then
import random
drawn=random.choice([k for k in cards for x in range(cards[k])])
#If it is the player's turn
player_cards[drawn]=1
#If it is the computer's turn
player_cards[drawn]=1
This code would run into problems if you draw the same card consecutively due to same index key. I would therefore argue the meaning of setting player_cards and computer_cards as dictionaries. If you want to allow drawing the same card, turning them into lists and simply appending drawn cards to them would be a lot easier.
Edit:
For the card to disappear from the general deck after being drawn to the player's/computer's deck,
cards[drawn]-=1