Startup Data scientist Blog

データ分析系のテック情報を発信します

pythonを使ってトランプの山札から5枚のカードを出すコード

 

import random

suits = ["Hearts", "Spades", "Clubs", "Diamonds"]
ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
deck =

for  suit in suits:
  for rank in ranks:
    deck.append(f'{rank} of {suit}')

print('There are 52 cards in the deck.')

print('Dealing ...')

hand =

while len(hand) < 5:
    card = random.choice(deck)
    deck.remove(card)
    hand.append(card)

print(f'There are {len(deck)} cards in the deck.')
print('Player has the following cards in their hand:')
print(hand)

 

 

Output

There are 52 cards in the deck.
Dealing ...
There are 47 cards in the deck.
Player has the following cards in their hand:
['5 of Clubs', '9 of Clubs', '10 of Diamonds', 'Jack of Clubs', '2 of Clubs']