2017-10-24 4 views
-4

コンピュータと人間の両方が同じカードを描いていると私はプログラムループを作ろうとしています。戦争の私のゲームです。開発中です。同じカードに着陸すれば、プログラムはループして、壊れた!私はそれを新しい段階に持っていましたが、これをpycharmで実行したときにエラーが発生しました。どうすればこのループを作るのですか?

今回は何が間違っているように見えますか、印刷する代わりにすべての値を変更しました。プログラム

import random 
while True: 
cardValue = random.randint(2,14) 
cardSuit = random.randint (0,3) 
humanCard = random.randint (2,14) 
humanSuit = random.randint (0,3) 


if cardValue == 14: 
    print ("Ace") 
elif cardValue == 13: 
    print ("King") 
elif cardValue == 12: 
    print ("Queen") 
elif cardValue == 11: 
    print ("Jack") 
elif cardValue < 10: 
    print (cardValue) 

if cardSuit == 0: 
    print ("Spades") 
elif cardSuit == 1: 
    print ("Diamonds") 
elif cardSuit ==2: 
    print ("Hearts") 
elif cardSuit == 3: 
    print *"Clubs" 


if humanCard == 14: 
    print ("Ace") 
elif humanCard == 13: 
    print ("King") 
elif humanCard == 12: 
    print ("Queen") 
elif humanCard == 11: 
    print ("Jack") 
elif humanCard < 10: 
    print (cardValue) 

if humanSuit == 0: 
    print ("Spades") 
elif humanSuit == 1: 
    print ("Diamonds") 
elif humanSuit == 2: 
    print ("Hearts") 
elif humanSuit ==3: 
    print ("Clubs") 


if humanCard > cardValue: 
    print("Human Wins!") 
    break 
elif humanCard < cardValue: 
    print('Computer Wins!') 
    break 
else: 
    print ('Its a draw!') 
    while True: 
     continue 
+0

私は関数内のコードをカプセル化し、その後humanCardで再帰的に呼び出すことを示唆しています= = cardValueの場合あなたが取得するタイの数を追跡する必要がある場合は、再帰関数から値を返します。 – David

+2

この質問に載せたタグを見てみましたか? 'war' - webアプリケーションアーカイブ、' tie' - perlコマンド。これらのタグはあなたの質問に当てはまると思いますか? –

+2

あなたは '真実 'について知っているようですので、**あなたのコード**のまわりでそれを使用してください。それは停止する必要があるとき '休憩' –

答えて

0
  1. で後でプリントは私はあなたがwhile (True)無限ループを使用している参照してください。無限ループを使用しても問題ありません。しかし、このコードwhile True: continueは、continueが単にが次の繰り返しに行くと言うので、あなたは永遠に立ち往生するでしょう。

  2. カードを引くあなたのコードは内部while (True)ループでなければなりません。あなたは新しいカードが各ラウンドで描かれるのが好きなので、以下は


あなたのコードの修正版です。慎重にそれをラインごとに理由づけます。

import random 

while True: 
    cardValue = random.randint(2,14) 
    cardSuit = random.randint(0,3) 
    humanCard = random.randint(2,14) 
    humanSuit = random.randint(0,3) 

    # do whatever you do to print the cards 

    if humanCard > cardValue: 
     print ("Human Wins!") 
     break # exit while loop 
    elif humanCard < cardValue: 
     print ("Computer Wins!") 
     break # exit while loop 
    else: # they draw same card 
     print ("It's a draw!") 
     # keep going 
     while True: 
     continue 
+0

私はcardValue = random.randint(2,14)を望んでいません。 –

+0

@Bobnarlyあなたは確かにそうです。タイプミスが修正されました。 – sam

+0

新しいコードを実行しているときにPythonでこのエラーが発生するTypeError:pycharmの 'str'と 'int'のインスタンス間で '>'がサポートされない –

1

私は、このようにそれを行うだろう(理由は、印刷の上f-stringsののpython 3.6が必要です):

import random 

suits  = ['Spades', 'Diamonds', 'Hearts', 'Clubs'] 
cards  = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace'] 
cards_values = [2,3,4,5,6,7,8,9,10,11,12,13,14] 

map_cards_values = dict(zip(cards,cards_values)) 
end_game = False 

while not end_game: 
    aiCard = random.choice(cards) 
    aiSuit = random.choice(suits) 
    humanCard = random.choice(cards) 
    humanSuit = random.choice(suits) 

    print(f"AI card is a {aiCard} of {aiSuit}") 
    print(f"Human card is a {humanCard} of {humanSuit}") 

    if map_cards_values[aiCard] != map_cards_values[humanCard]: 
     end_game=True 
関連する問題