Python随机卡经销商 - 面部卡计数器不能正确计数

时间:2016-07-22 20:31:50

标签: python

我正在练习为课堂写这个课程。我必须处理牌,直到发出四张A,最后,我还要计算有多少面牌(杰克,皇后,国王牌)被处理。我没有为卡片名称制作字典,因为我的老师特意告诉我们做随机整数命令。但是,除了面部计数器(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

2 个答案:

答案 0 :(得分:0)

我想我找到了它。考虑你已经滚动面部卡的情况。

  

设r = 11.

     

排名 - &gt;杰克'

     

西装 - &gt;任何

     

打印('杰克什么')

     

增加f_counter

     

//下一次迭代

     

设r = 1

     

通过设置排名的if语句,因为排名不是11,12,13或者&gt; 1,因此rank ='Jack'

     

打印('其他西装的杰克')

     

增加a_counter(因为滚动r = 1)

     

摒弃增加f_counter的可能性

因此,您在不增加f_counter的情况下打印了一张面卡。

答案 1 :(得分: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

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