【发布时间】:2016-07-23 05:14:30
【问题描述】:
我正在练习为课堂编写这个程序。我必须发牌直到发完四个 A,最后,我还必须计算发了多少张牌(J、Q、K)。我没有为卡片名称制作字典,因为我的老师特别告诉我们要执行随机整数命令。但是,除了人脸计数器 (f_counter) 之外,一切都正常。由于某种原因,它总是少一张脸牌。有谁知道为什么?谢谢!
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 不会设置并在 print 语句中引发错误。
-
每次结果都会不同,因为每次都有不同数量的面卡。我自己数了几张脸牌,结果总是和 f_counter 说的有点偏差。
-
你是在考虑甲板的大小还是不重要?
-
那不是;他说每张牌都是完全独立于前一张的。
标签: python