I have lists;
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_cards = []
And i have a function will deal cards.
def deal_cards():
player_cards.append(random.choice(cards))
player_cards.append(random.choice(cards))
I'm trying to make a blackjack game. And just want to pick random cards for the player twice using for loop or some another method. So how can i make it?
CodePudding user response:
Use sample from the random module. There, you can specify the number of elements you want to pick at random from a given set. Elements are sampled without replacement.
player_cards = random.sample(cards,2)
CodePudding user response:
Simple code based on Simon Hawe solution can be as below:
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 12, 13, 14, 15, 16]
def deal_cards():
return random.sample(cards, 2)
player_cards = deal_cards()
print(f'Player have a cards: {player_cards} from possible list {cards}')
