2016-07-22 51 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