【问题标题】:Python random card dealer - face card counter doesn't count correctlyPython随机发牌员 - 面牌计数器不正确计数
【发布时间】: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


【解决方案1】:

我想我找到了。考虑一下你有一张人脸卡的情况。

令 r = 11。

排名 -> '杰克'

西装 -> 任何东西

print('随便什么的杰克')

增加 f_counter

// 下一次迭代

令 r = 1

通过设置 rank 的 if 语句,因为 rank 不是 11,12,13 或 > 1,因此 rank = 'Jack'

print('其他西装的杰克')

增加a_counter(因为roll r = 1)

消除增加 f_counter 的可能性

因此,您打印了一张没有增加 f_counter 的面卡。

【讨论】:

    【解决方案2】:

    我对您的程序进行了一些更改。让我知道这是否能提供您想要的输出,我可以解释主要变化。

    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
    

    这似乎对我有用,除了你的程序中没有任何东西可以防止同一张卡出现两次(或更多)......

    【讨论】:

    • 非常感谢!我把你的打印语句换成了我的,因为你的打印语句由于某种原因没有打印,但其他一切正常(你的 elif 语句比我的更有意义)并且 f_counter 现在计数正确。我有一个问题,但“rank = ''”有什么意义?
    • @user6627144 - 太好了 - 如果它达到了您的要求,请随时接受它作为正确答案
    • @user6627144 我会尽量坚持使用我的打印语句,因为它是一种更像 Python 的打印方式。
    • @user6627144 - rank = '' 在循环之外声明排名(就像您对其他变量所做的那样)
    猜你喜欢
    • 2012-03-03
    • 1970-01-01
    • 2015-10-01
    • 2016-08-18
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多