0

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
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
l.m
  • 131
  • 1
  • 10
  • Are you aiming to calculate the probability (analytically or by simulation)? Or just to output a constant? – Reinderien Sep 25 '19 at 03:04
  • @Reinderien I want to output it to the screen. So as an example ```print("The probability of getting a pair is ", count)``` count being the amount of hands – l.m Sep 25 '19 at 03:06
  • Add the `while` before `cards = []` because you need to repeat that entire process of resetting and selecting your cards. – MyNameIsCaleb Sep 25 '19 at 03:07
  • @MyNameIsCaleb It would just be ```while(True)``` and return false when one of the if statements executes? – l.m Sep 25 '19 at 03:08

1 Answers1

1

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}')
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • Gotcha, then do count+=1 at the end matching up with the while(true) statement? – l.m Sep 25 '19 at 03:11
  • Yes, at the bottom before it loops. I'll clarify – MyNameIsCaleb Sep 25 '19 at 03:11
  • @l.m I updated it for you. Obviously more work to go but that should get you going. If that answers your question please vote and hit the checkmark next to it to close out the question. – MyNameIsCaleb Sep 25 '19 at 03:17
  • If you want to return the count instead, you could replace each break with `return count` and then do any stats in a different function. That would allow you to remove the `stop` flag that I added. You would still need the `count` there adding at the bottom. – MyNameIsCaleb Sep 25 '19 at 03:19
  • 1
    You are the man! Its printing count now thats what im going to use to make it print the probability. – l.m Sep 25 '19 at 03:21
  • Hey question to get two pairs how would that work since I have if number equalling to the number of times that number shows up but now I want it for two numbers. – l.m Sep 25 '19 at 04:06
  • You would need to evaluate for two pairs differently. You could set a pair flag and then not break until the end of those loops and print the pair then. You could also figure a way to check for two pairs before you check one pair. Something like [this]( https://stackoverflow.com/questions/12282232/how-do-i-count-unique-values-inside-a-list) probably would be worth looking into. – MyNameIsCaleb Sep 25 '19 at 04:10