2016-07-22 54 views
0

我正在练习编写这个课程。我必须处理扑克牌,直到发出四张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 
+0

我跑了几次,得到了8,作为前几个结果的是16,30和19。尽管如此,如果你的第一卷r是1,rank并没有被设置并且在打印语句中抛出一个错误,你似乎有一个bug。 – dashiell

+0

每个结果都会有所不同,因为每次都有不同数量的面卡。我自己数着面子牌,而且他们总是和f_counter说的有点不一样。 – user6627144

+0

你在考虑甲板的大小还是不重要? – Carlos

回答

0

我想我找到了它。考虑一下你有面子牌的情况。

设r = 11

排名 - > '杰克'

套装 - >什么

打印( '无论杰克')

增量f_counter

//下一次迭代

我们通过如果秩秩因为不是11,12,13或> 1,因此排名语句=“杰克”

打印(“其他一些诉讼插孔R = 1

秋季)

增量a_counter(因为轧制R = 1)

秋季通过的可能性f_counter

递增因此,您已印刷的面卡不增加f_counter。

0

我对你的程序做了一些修改。让我知道这是否能给出你想要的结果,并且我可以解释主要的变化。

rank = '' 

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 s == 1: 
     suit = "Spades" 
    elif s == 2: 
     suit = "Hearts" 
    elif s == 3: 
     suit = "Diamonds" 
    elif s == 4: 
     suit = "Clubs" 

    if r == 11: 
     rank = "Jack" 
     f_counter += 1 
    elif r == 12: 
     rank = "Queen" 
     f_counter += 1 
    elif r == 13: 
     rank = "King" 
     f_counter += 1 
    elif r > 1 and r < 11: 
     rank = r 
    elif r == 1: 
     rank == "Ace" 
     a_counter += 1 

    if r == 1: 
     print("Card %d: An Ace of %s! **") % (t_counter, suit) 
    else: 
     print("Card %d: a %s of %s") % (t_counter, rank, suit) 

    if a_counter == 4: 
     print("You got %d face cards!") % (f_counter) 
     break 

这似乎是为我工作,但没有什么在你的程序将防止同一张卡调高两次(或更多)...

+0

非常感谢!我把你的打印语句与我的打印语句切换,因为你的打印出于某种原因,但是其他所有的东西都能正常工作(你的ELIF语句比我的更有意义),而f_counter现在正在计数。我有一个问题,但是“rank =''”是什么意思? – user6627144

+0

@ user6627144 - 太好了 - 如果它达到你想要的效果,请随时接受它作为正确的答案。 – fugu

+0

@ user6627144我会尝试坚持使用我拥有的打印语句,因为它是更加Python的打印方式。 – fugu