0

I am trying to append random values and hold it in another value from a dictionary. As for a flush you need to clarify suits, so I did so for the dictionary, then I wanted to add random values between 1-14 for a hand of five cards to see if it is all of the same suits. Then if so print its a flush then use count to print the probability of the number of hands it took to get a flush.

The issue I am having is appending the random dictionary values then checking if it is a flush, im not sure if the code I have will do so correctly because it was used for hardcoded values not a random generator now.

def flush():
    count = 0
    while (True):
        values = {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":11, "Q":12, "K":13, "A":14}
        for i in range (5):
            hand = values.append(random.choice([2,3,4,5,6,7,8,9,10,11,12,13,14]))
        stop = False
        for value in values:
            suits = [h[1] for h in hand]
            if len(set(suits)) == 1:
              return True
            else:
              return False
        if stop:
            break
        else:
            count+=1
    print(f'Count is {1/count}')
l.m
  • 131
  • 1
  • 10

1 Answers1

2

It seems you don't even have a "Suit" on your cards, so how will you detect a Flush?

Here is an example for a nice "Deck Of Cards" and detecting a Flush. Combine that with the collections.Counter I suggested in your other question, and you will have most of the combinations.

I'd suggest to read up on objects and classes (e.g. http://introtopython.org/classes.html)

And I'll leave it to you to create a class Hand, which takes 5 cards in __init__ and has methods like is_flush()...

from collections import namedtuple
from random import shuffle

Card = namedtuple("Card", "suit, rank")

class Deck:
    suits = '♦♥♠♣'
    ranks = '23456789JQKA'

    def __init__(self):
        self.cards = [Card(suit, rank) for suit in self.suits for rank in self.ranks]
        shuffle(self.cards)

    def deal(self, amount):
        return tuple(self.cards.pop() for _ in range(amount))



flush = False
while not flush:
    deck = Deck()

    while len(deck.cards) > 5:
        hand = deck.deal(5)
        # (Card(suit='♣', rank='7'), Card(suit='♠', rank='2'), Card(suit='♥', rank='4'), Card(suit='♥', rank='K'), Card(suit='♣', rank='3'))

        if len(set(card.suit for card in hand)) > 1:
            print(f"No Flush: {hand}")
            continue

        print(f"Yay, it's a Flush: {hand}")
        flush = True
        break

# No Flush: (Card(suit='♠', rank='K'), Card(suit='♣', rank='6'), Card(suit='♦', rank='6'), Card(suit='♣', rank='4'), Card(suit='♠', rank='Q'))
# No Flush: (Card(suit='♠', rank='J'), Card(suit='♥', rank='9'), Card(suit='♥', rank='3'), Card(suit='♣', rank='2'), Card(suit='♠', rank='6'))
# No Flush: (Card(suit='♣', rank='7'), Card(suit='♥', rank='5'), Card(suit='♦', rank='5'), Card(suit='♦', rank='Q'), Card(suit='♦', rank='K'))
# Yay, it's a Flush: (Card(suit='♣', rank='3'), Card(suit='♣', rank='A'), Card(suit='♣', rank='J'), Card(suit='♣', rank='9'), Card(suit='♣', rank='5'))
Sebastian Loehner
  • 1,302
  • 7
  • 5
  • 1
    Please, don't ever include special characters in your code. It's just bad practice and especially beginners should not be taught that this is generally accepted. – 3ch0 Sep 25 '19 at 08:23