2016-07-22 11 views
0

私はクラス用のこのプログラムの作成を練習しています。私は4枚のエースが処理されるまでカードを扱わなければなりません。そして最後に、いくつのカード(ジャック、クイーン、キングカード)が扱われたかを数えなければなりません。教師がランダム整数コマンドを行うよう具体的に指示したので、私はカード名の辞書を作っていませんでした。しかし、すべてが顔カウンタ(f_counter)を除いて動作します。それは何らかの理由で常に1枚少ないカードを数えます。なぜ誰が知っていますか?ありがとう!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の場合、ランクが設定されず、printステートメントでエラーをスローするバグがあるようです。 – dashiell

+0

毎回異なる枚数の顔カードがあるため、結果はそれぞれ異なります。私は自分自身で顔のカードを数え、彼らはいつもf_counterが言ったことからちょっと離れていた。 – user6627144

+0

デッキの大きさを考えているのですか、それとも重要ではないですか? – Carlos

答えて

0

私はそれを見つけたと思う。フェイスカードを転がした場合を考えてみましょう。

レッツのR = 11

ランク - > 'ジャック

スーツ - >何も

印刷( '何のジャック・')

インクリメント

f_counter

//次の繰り返し

もしランクが11,12,13または> 1、これランクではないので、ランクを設定文= 'ジャック

印刷(「他のいくつかのスーツのジャック」を介して、R = 1

秋をしてみましょうロールR = 1)の可能性を通して

秋にはこのように

f_counter増分するため)

インクリメントa_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は現在正しくカウントしています。私が持っていた1つの質問は、「ランク= ''」のポイントは何ですか? – user6627144

+0

@ user6627144 - これは素晴らしいことです - あなたが望むものが達成されれば、正解としてそ​​れを受け入れることを自由に感じてください。 – fugu

+0

@ user6627144これはもっとPythonの印刷方法ですので、私が持っている印刷ステートメントを試してみます。 – fugu

関連する問題