检查手牌中是否有两对 Python
Checking if theres two pairs in card hand Python
我正在尝试检查随机手中是否有两对。
我现在有它,它会打印一对,所以它会打印那张卡片出现的次数,所以如果有 2 个,它将是 2 x 2
,所以第一个数字是出现,然后第二个数字是卡号然后打印一对。
我如何让它打印两对而不是所以检查一手 5 是否有让我们说例如 2 x 2
和 2 x 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}')
查看 collections
模块中的 Counter
。
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()
我正在尝试检查随机手中是否有两对。
我现在有它,它会打印一对,所以它会打印那张卡片出现的次数,所以如果有 2 个,它将是 2 x 2
,所以第一个数字是出现,然后第二个数字是卡号然后打印一对。
我如何让它打印两对而不是所以检查一手 5 是否有让我们说例如 2 x 2
和 2 x 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}')
查看 collections
模块中的 Counter
。
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()