我正在練習編寫這個課程。我必須處理撲克牌,直到發出四張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
我跑了幾次,得到了8,作爲前幾個結果的是16,30和19。儘管如此,如果你的第一卷r是1,rank並沒有被設置並且在打印語句中拋出一個錯誤,你似乎有一個bug。 – dashiell
每個結果都會有所不同,因爲每次都有不同數量的面卡。我自己數着面子牌,而且他們總是和f_counter說的有點不一樣。 – user6627144
你在考慮甲板的大小還是不重要? – Carlos