Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 配对概率_Python_Random_Poker - Fatal编程技术网

Python 配对概率

Python 配对概率,python,random,poker,Python,Random,Poker,我正在尝试做一个扑克游戏,它会检查它是一对还是三对还是四对 我试图找出在while循环中插入的位置。如果我应该将它放在集合中的卡的前面:语句或范围(5)中的I的语句前面: 我想继续打印5张卡片,直到它显示一对、3张一类或4张一类 然后我想做的是打印这些选项之一的概率 import random def poker(): cards = [] count = 0 for i in range(5): cards.append(random.choice([1

我正在尝试做一个扑克游戏,它会检查它是一对还是三对还是四对

我试图找出在while循环中插入
的位置。如果我应该将它放在集合中的卡的
前面:
语句或范围(5)中的I的
语句前面:

我想继续打印5张卡片,直到它显示一对、3张一类或4张一类

然后我想做的是打印这些选项之一的概率

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

您应该将
while
放在卡的上方,但将
count
放在该循环之外,以便维护它。您这样做是因为您每次都需要重复整个卡片创建/选择过程,直到您满足所需的条件

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}')

你的目标是计算概率(分析还是模拟)?还是仅仅输出一个常量?@Reinderien我想把它输出到屏幕上。例如,
print(“获得一对的概率是”,count)
count是手的数量在
cards=[]]
之前添加
while
,因为您需要重复重置和选择卡的整个过程。@MyNameIsCaleb它应该是
while(True)
并在其中一个if语句执行时返回false?Gotcha,然后在结尾处与while(true)语句匹配时执行count+=1?是,在循环之前在底部执行。我将clarify@l.m我帮你更新了。显然还有更多的工作要做,但这应该会让你走。如果这回答了您的问题,请投票并点击旁边的复选标记结束问题。如果您想返回计数,您可以用
return count
替换每个中断,然后在不同的函数中执行任何统计。这将允许您删除我添加的
stop
标志。您仍然需要在底部添加
计数
。您就是那个人!现在它的打印计数,这就是我要用它来打印概率。