我正在练习编写这个课程。我必须处理扑克牌,直到发出四张A牌为止,最后,我还必须计算处理了多少张面牌(插孔,皇后,国王牌)。我没有为卡名写字典,因为我的老师特意告诉我们做随机整数命令。然而,除了脸部计数器(f_counter)之外,一切都可以工作。出于某种原因,它总是少计一张面卡。有谁知道为什么?谢谢!Python随机卡片经销商 - 面卡计数器不能正确计数
print("You were dealt:\n")
import random
# This is the initial counter for the number of cards dealt.
t_counter = 0
# This is the initial counter for the number of aces dealt.
a_counter = 0
# This is the initial counter for the number of face cards dealt.
f_counter = 0
# This is so both a rank and a suit are dealt.
r = random.randint(1,13)
s = random.randint(1,4)
while a_counter < 4:
# This counts and tells the user of each card dealt that isn't an ace.
r = random.randint(1,13)
s = random.randint(1,4)
t_counter += 1
if r == 11:
rank = "Jack"
elif r == 12:
rank = "Queen"
elif r == 13:
rank = "King"
elif r > 1:
rank = r
if s == 1:
suit = "Spades"
elif s == 2:
suit = "Hearts"
elif s == 3:
suit = "Diamonds"
elif s == 4:
suit = "Clubs"
print("Card",t_counter,': A',rank,"of",suit,)
# This counts the aces.
if r == 1:
a_counter += 1
print("An Ace of",suit,"!")
# This counts the face cards.
if r == 11 or r == 12 or r == 13:
f_counter += 1
# This allows up to four aces and also prints the number of face cards as the last thing.
if a_counter == 4:
print("You got",f_counter,"face cards!")
break
我跑了几次,得到了8,作为前几个结果的是16,30和19。尽管如此,如果你的第一卷r是1,rank并没有被设置并且在打印语句中抛出一个错误,你似乎有一个bug。 – dashiell
每个结果都会有所不同,因为每次都有不同数量的面卡。我自己数着面子牌,而且他们总是和f_counter说的有点不一样。 – user6627144
你在考虑甲板的大小还是不重要? – Carlos