Home > Enterprise >  How can I shuffle my deck of cards repeatedly?
How can I shuffle my deck of cards repeatedly?

Time:10-31

We have to create a code that will shuffle the deck of cards after showing the first 13 cards.

so far this is the portion of my code:

deck_of_cards = [ "A-C", "2-C" ,"3-C" ,"4-C" ,"5-C" ,"6-C" ,"7-C" ,"8-C" ,"9-C" ,"10-C" ,"J-C" ,"Q-C" ,"K-C" ,
                 "A-D" ,"2-D" ,"3-D" ,"4-D" ,"5-D" ,"6-D" ,"7-D" ,"8-D" ,"9-D" ,"10-D" ,"J-D" ,"Q-D" ,"K-D" ,
                 "A-H" ,"2-H" ,"3-H" ,"4-H" ,"5-H" ,"6-H" ,"7-H" ,"8-H" ,"9-H" ,"10-H" ,"J-H" ,"Q-H" ,"K-H" ,
                 "A-S" ,"2-S" ,"3-S" ,"4-S" ,"5-S" ,"6-S" ,"7-S" ,"8-S" ,"9-S" ,"10-S" ,"J-S" ,"Q-S" ,"K-S"]

if choose == '1'
    new_deck = []
    d1 = deck_of_cards[:len(deck_of_cards)//2]
    d2 = deck_of_cards[len(deck_of_cards)//2::]
    for i in range(len(deck_of_cards)//2):
        new_deck.append(d2[i])
        new_deck.append(d1[i])
    if len(deck_of_cards) % 2 == 1:
        new_deck.append(deck_of_cards[-1])
        
    shuffle = new_deck[0:13:]

    print(','.join(map(str,shuffle)))

I got the shuffled deck of cards (13 cards) but if I shuffle it twice, its still the same set of cards:

A-H,A-C,2-H,2-C,3-H,3-C,4-H,4-C,5-H,5-C,6-H,6-C,7-H

How can I shuffle the deck of cards repeatedly without getting the same set of shuffled cards just like the above result???

We were also asked not to import module functions.

I am just a beginner coder.

CodePudding user response:

With the below code snippet via using itertools and random modules, I draw 13 new cards for each draw:

import itertools, random

deck = list(itertools.product(range(1,14),['-S','-H','-D','-C']))

random.shuffle(deck)

# draw 13 cards
print("You got:")
for i in range(13):
   print(f"{deck[i][0]}{deck[i][1]}")

CodePudding user response:

If you need to shuffle the cards repeatedly, you can use a loop. It may also help if the shuffling is a function.

deck_of_cards = [ "A-C", "2-C" ,"3-C" ,"4-C" ,... ,"Q-S" ,"K-S"]

def shuffle(a_deck):
    do stuff that make a new deck by shuffling a_deck
    return shuffled_deck

while True:    # or some other condition - this will run forever
    deck_of_cards = shuffle(deck_of_cards)
    # show the first thirteen cards with a slice
    print(deck_of_cards[:13])
    q = input('take some time to look at those cards before shuffling again - then press ENTER')

You may want to add code that will stop the loop on some condition.

  • Related