我正在尝试检查一手中是否有两对随机。我现在有它在哪里打印一对,所以它打印出该卡的出现次数,所以如果有2个二进制,它将是2 x 2
,所以第一个数字是出现的次数,然后第二个数字是卡号然后打印一对。
我如何使其打印两对,所以例如检查一下是否有2 x 2
和2 x 5
这样的5对,然后一对2和5然后打印出"two pairs"
。
我在numbers = cards.count(card)
中添加了内容,并在其下面的if语句中添加了numbers == 2
因此,如果有一对和一对,它将打印两对以及得到它的可能性。
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
for card in cards:
number = cards.count(card) # Returns how many of this card is in your hand
numbers = cards.count(card)
print(f"{number} x {card}")
if(number == 2 and numbers == 2):
print("One Pair")
stop = True
break
if stop:
break
else:
count += 1
print(f'Count is {count}')
查看Counter
模块中的collections
。https://docs.python.org/3/library/collections.html#collections.Counter
import random
from collections import Counter
def twopair():
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]))
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): "Whoops, who's cheating",
(2, 2): "Two Pairs",
(3, 2): "Full House",
}
msg = count_to_message[count]
print(msg)
if msg == "Two Pairs":
break
twopair()