jeudi 25 mai 2017

Generating a number or string that has not been generated before in python

I am creating a black jack inspired game and i generate the deck and hand using the following code:

suits = 'SCHD'
values = '23456789TJQKA'

deck = tuple(''.join(card) for card in itertools.product(values, suits))

dealershand = random.sample(deck, 1)
yourhand    = random.sample(deck, 2)

The problem with this is that there is a small chance that the same card is pulled in the 'dealershand' and 'yourhand' I would like to check if the card already exists, and if it does, to generate another card. Something like this:

while yourhand is in dealershand:
    yourhand=random.sample(deck,2)

any ideas guys???




Aucun commentaire:

Enregistrer un commentaire