l.m
l.m

Reputation: 131

Python probability of a pair

I am trying to make a poker game where it would check if it is a pair or three of a kind or four of a kind.

I am trying to figure out where to insert a while loop. if I should put it in front of the for card in set(cards): statement or the for i in range(5):

I want to keep printing 5 cards until it shows either a pair, 3 of a kind, or 4 of a kind.

Then what I want to do is to print the probability of printing one of those options

import random
def poker():
    cards = []
    count = 0
    for i in range(5):
        cards.append(random.choice([1,2,3,4,5,6,7,8,9,10,11,12,13]))
        print(cards)
    for card in set(cards):
        number = cards.count(card) # Returns how many of this card is in your hand
        print(f"{number} x {card}")
        if(number == 2):
            print("One Pair")
            break
        if(number == 3):
            print("Three of a kind")
            break
        if(number == 4):
            print("Four of a kind")
            break

Upvotes: 0

Views: 488

Answers (1)

MyNameIsCaleb
MyNameIsCaleb

Reputation: 4489

You should put the while above cards but bring count outside of that loop so you can maintain it. You do this because you need to repeat the entire card creation/selection process each time until you meet the condition you are looking for.

import random
def poker():
    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]))
            print(cards)
        stop = False
        for card in cards:
            number = cards.count(card) # Returns how many of this card is in your hand
            print(f"{number} x {card}")
            if(number == 4):
                print("Four of a kind")
                stop = True
                break
            elif(number == 3):
                print("Three of a kind")
                stop = True
                break
            elif(number == 2):
                print("One Pair")
                stop = True
                break
        if stop:
            break
        else:
            count += 1
    print(f'Count is {count}')

Upvotes: 1

Related Questions