-1

I am trying to make a simple poker game where I would simulate each hand shown in the options as no pair, one pair, two pair, etc.

I am trying to print out the probability by using count to increment each time it deals a new hand

def twopair():
    count = 0
    while True:
        cards = []
        for i in range(5):
            cards.append(random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]))
        stop = False 
        counted_cards = Counter(cards)
        two_most_common, count = zip(*counted_cards.most_common(2))

        count_to_message = {
            (1, 1): "Nothing",
            (2, 1): "One Pair",
            (3, 1): "Three of a Kind",
            (4, 1): "Four of a Kind",
            (5, 1): "Five of a Kind",
            (2, 2): "Two Pairs",
            (3, 2): "Full House",
        }

        msg = count_to_message[count]
        print(msg)
        if msg == "Two Pairs":
            stop = True
            break
        #else:
         #   count+=1
    #print(f'Count is {1/count}')

If you remove the # it gives a error of cant concatenate a tuple with a int. What could i do to resolve this to make a count so i can divide count by 1 to give me the probability of getting two pairs?

Mac
  • 1
  • Can you post you count variable after two_most_common, count = zip(*counted_cards.most_common(2)) line? – Wonka Sep 25 '19 at 16:06
  • what do you expect this `zip(*counted_cards.most_common(2))` to do? – njzk2 Sep 25 '19 at 16:07
  • A solution can be two_most_common, count = zip(*counted_cards.most_common(2))[0] (Cause its a list, test if its your desired output) – Wonka Sep 25 '19 at 16:14
  • Note: your hand selection code could result in a 5-of-a-kind, which is not possible in poker (without wild cards). An alternative might be: `cards = [x % 13 for x in random.sample(range(1, 53), 5)]` – 001 Sep 25 '19 at 16:45

2 Answers2

1

A simple print command shows that you've overloaded your variable count: you're trying to use it as both a tuple of card counts and a count of how many times you've encountered 2 pair. You need two separate variables for the two simultaneous concepts.

I fixed a couple other problems with the code:

import random
from collections import Counter

pair_count = 0
while True:
    cards = []
    for i in range(5):
        cards.append(random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]))
    stop = False 
    counted_cards = Counter(cards)
    two_most_common, count = zip(*counted_cards.most_common(2))
    print(two_most_common, count)

    count_to_message = { 
        (1, 1): "Nothing",
        (2, 1): "One Pair",
        (3, 1): "Three of a Kind",
        (4, 1): "Four of a Kind",
        (5, 1): "Five of a Kind",
        (2, 2): "Two Pairs",
        (3, 2): "Full House",
    }   

    msg = count_to_message[count]
    print(msg)
    if msg == "Two Pairs":
        break

    pair_count+=1

print('Count is', 1/pair_count)

Output:

(4, 6) (1, 1)
Nothing
(5, 9) (1, 1)
Nothing
(7, 8) (2, 1)
One Pair
(10, 11) (2, 1)
One Pair
(12, 6) (2, 1)
One Pair
(7, 13) (2, 1)
One Pair
(11, 4) (1, 1)
Nothing
(13, 7) (2, 1)
One Pair
(2, 1) (1, 1)
Nothing
(12, 13) (2, 2)
Two Pairs
Count is 0.1111111111111111

You still have a couple of problems, most notably that this is not an accurate metric for the probability of getting two pairs.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

Couter.most_common() returns a list of tuples (element, count): https://docs.python.org/2/library/collections.html

And when you write:

>>> two_most_common, count = zip(*counted_cards.most_common(2))
>>> type(count)
<class 'tuple'>
>>>

count doesn't remain an int. So, count+=1 throws the error.

Since count is a tuple, you can access values inside it with count[0], count[1] depending on what you are trying to do.

brokenfoot
  • 11,083
  • 10
  • 59
  • 80