I have that code. And If I enter n argument is 3 it will be 3 hands. but I want to use any card only one time. totally there 52 cards. and each card can be use just one time. How to I delete the cards after the use?
by the way, stdio.writeln is like print. same thing.
n = int(sys.argv[1])
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen",
"King", "Ace"]
rank = random.randrange(0, len(RANKS))
suit = random.randrange(0, len(SUITS))
deck = []
for rank in RANKS:
for suit in SUITS:
card = rank + " of " + suit
deck += [card]
for i in range(n):
a = len (deck)
for i in range(a):
r = random.randrange(i, a)
temp = deck[r]
deck[r] = deck[i]
deck[i] = temp
for i in range(5):
stdio.writeln(deck[i] + " ")
stdio.writeln()
Leverage List comprehenshion to compose a full stack of cards, get a copy of mixed card from it using random.sample() and draw cards up by list.pop() which will remove the card automatically.
I reduced the RANKS
a bit to shorten the print-outs:
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6"] #,'7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
import random
# in order
StackOfCards = [ f'{r} of {s}' for r in RANKS for s in SUITS]
# return random.sample() of full length input == random shuffled all cards
mixedDeck = random.sample(StackOfCards, k = len(StackOfCards))
print("Orig:",StackOfCards)
print()
print("Mixed:",mixedDeck)
print()
# draw 6 cards into "myHand" removing them from "mixedDeck":
myHand = [mixedDeck.pop() for _ in range(6)]
print("Hand:",myHand)
print()
print("Mixed:",mixedDeck)
Output:
Orig: ['2 of Clubs', '2 of Diamonds', '2 of Hearts', '2 of Spades', '3 of Clubs',
'3 of Diamonds', '3 of Hearts', '3 of Spades', '4 of Clubs', '4 of Diamonds',
'4 of Hearts', '4 of Spades', '5 of Clubs', '5 of Diamonds', '5 of Hearts',
'5 of Spades', '6 of Clubs', '6 of Diamonds', '6 of Hearts', '6 of Spades']
Mixed: ['2 of Clubs', '3 of Diamonds', '6 of Hearts', '4 of Spades', '5 of Clubs',
'3 of Hearts', '6 of Spades', '5 of Hearts', '4 of Diamonds', '3 of Spades',
'2 of Spades', '6 of Clubs', '4 of Clubs', '5 of Spades', '6 of Diamonds',
'2 of Diamonds', '3 of Clubs', '2 of Hearts', '5 of Diamonds', '4 of Hearts']
Hand: ['4 of Hearts', '5 of Diamonds', '2 of Hearts',
'3 of Clubs', '2 of Diamonds', '6 of Diamonds']
Mixed: ['2 of Clubs', '3 of Diamonds', '6 of Hearts', '4 of Spades', '5 of Clubs',
'3 of Hearts', '6 of Spades', '5 of Hearts', '4 of Diamonds', '3 of Spades',
'2 of Spades', '6 of Clubs', '4 of Clubs', '5 of Spades']
Using .pop()
alleviated the need of removing something, if you really dislike it (why would you?) you can recreate a list without elements of another list by:
l = [1,2,3,4,5,6]
p = [2,6]
l[:] = [ x for x in l if x not in p] # inplace modifies l to [1,3,4,5]