python 中使用纸牌的优惠券收集器问题
Coupon collector's problem in python using deck of cards
在编写一个程序时遇到问题,该程序可以模拟在获得每套花色之前需要从一副牌中挑选的数量。我 运行 进入了一个无限循环,我不确定如何摆脱它并获得我需要的输出。
import random
def coupon_collector():
suits=['Hearts','Diamonds','Spades','Clubs']
cards=['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
def has_suit(cards,suit):
for s in cards:
if suit == s:
return True
return False
def has_all_suits(cards):
for suit in suits:
if not has_suit(cards,suit):
return False
return True
def main():
deck=[]
my_cards=[]
j=0
for i in range(52):
deck.append(suits[i%4]+cards[int(i/4)])
while not has_all_suits(my_cards):
ind=random.randint(0,51)
card=deck[ind]
if not has_suit(my_cards,card[0]):
my_cards.append(card)
j+=1
for card in my_cards:
print(card[1],'of',card[0])
print('Number of picks:',j)
main()
coupon_collector()
问题是 has_suit
永远不会 returns 为真,因为 suit == s
永远不会为真。 mycards
列表中的每个元素都是一串与花色相连的牌值(例如 "Diamonds3")。这意味着您正在检查 "Diamonds" == "Diamonds3"
等。相反,您应该检查 if suit in s
以查看字符串 "Diamonds" 是否包含在字符串 "Diamonds3".
另外一个函数里面的函数定义有点奇怪,没必要。我编辑了你的代码:
import random
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
cards = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
def has_suit(cards, suit):
for s in cards:
if suit in s:
return True
return False
def has_all_suits(cards):
for suit in suits:
if not has_suit(cards, suit):
return False
return True
def main():
deck = []
my_cards = []
j = 0
for i in range(52):
deck.append(suits[i%4] + cards[int(i/4)])
while not has_all_suits(my_cards):
ind = random.randint(0, 51)
card = deck[ind]
if not has_suit(my_cards, card[0]):
my_cards.append(card)
j += 1
for card in my_cards:
print(card)
print('Number of picks:', j)
main()
在编写一个程序时遇到问题,该程序可以模拟在获得每套花色之前需要从一副牌中挑选的数量。我 运行 进入了一个无限循环,我不确定如何摆脱它并获得我需要的输出。
import random
def coupon_collector():
suits=['Hearts','Diamonds','Spades','Clubs']
cards=['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
def has_suit(cards,suit):
for s in cards:
if suit == s:
return True
return False
def has_all_suits(cards):
for suit in suits:
if not has_suit(cards,suit):
return False
return True
def main():
deck=[]
my_cards=[]
j=0
for i in range(52):
deck.append(suits[i%4]+cards[int(i/4)])
while not has_all_suits(my_cards):
ind=random.randint(0,51)
card=deck[ind]
if not has_suit(my_cards,card[0]):
my_cards.append(card)
j+=1
for card in my_cards:
print(card[1],'of',card[0])
print('Number of picks:',j)
main()
coupon_collector()
问题是 has_suit
永远不会 returns 为真,因为 suit == s
永远不会为真。 mycards
列表中的每个元素都是一串与花色相连的牌值(例如 "Diamonds3")。这意味着您正在检查 "Diamonds" == "Diamonds3"
等。相反,您应该检查 if suit in s
以查看字符串 "Diamonds" 是否包含在字符串 "Diamonds3".
另外一个函数里面的函数定义有点奇怪,没必要。我编辑了你的代码:
import random
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
cards = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
def has_suit(cards, suit):
for s in cards:
if suit in s:
return True
return False
def has_all_suits(cards):
for suit in suits:
if not has_suit(cards, suit):
return False
return True
def main():
deck = []
my_cards = []
j = 0
for i in range(52):
deck.append(suits[i%4] + cards[int(i/4)])
while not has_all_suits(my_cards):
ind = random.randint(0, 51)
card = deck[ind]
if not has_suit(my_cards, card[0]):
my_cards.append(card)
j += 1
for card in my_cards:
print(card)
print('Number of picks:', j)
main()