2017-11-19 8 views
0

R/P/Sプログラムの各ラウンドで各プレイヤーが何をしているかを記録しています。おそらく、無限ループを繰り返す方法がありますか?ここでは、コードwhileループ中のラウンドを追跡する方法

import random 
Round = 0 
Player_Score = 0 
Computer_Score = 0 
while Player_Score < 5 and Computer_Score < 5: 
    Player_object = input("Would you like to choose R, P, or S?") 
    Computer_object = random.sample("RPS", 1)[0] 
    if Player_object == "R" or Player_object == "r": 
     if Computer_object == "R": 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.") 
     elif Computer_object == "P": 
      Computer_Score += 1 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have been beaten by the Computer and it has scored a point.") 
     else: 
      Player_Score += 1 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.") 


    if Player_object == "P" or Player_object == "p": 
     if str(Computer_object) == "R": 
      Player_Score += 1 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.") 
     elif str(Computer_object) == "P": 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have tied with the Computer and neither of you have scored a point.") 
     else: 
      Computer_Score += 1 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.") 


    if Player_object == "S" or Player_object == "s": 
     if str(Computer_object) == "R": 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.") 
     elif str(Computer_object) == "P": 
      Computer_Score += 1 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have beaten the Computer and you have scored a point.") 
     else: 
      Player_Score += 1 
      print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.") 
if Computer_Score == 5 and Player_Score != 5: 
    print("The Computer has won!") 
if Player_Score == 5 and Computer_Score != 5: 
    print("You have won and beaten the computer") 
R = "Rock" 
r = "Rock" 
P = "Paper" 
p = "Paper" 
S = "Scissors" 
s = "Scissors" 
+0

あなたの 'while'ループが始まる前にリストを作成し、各結果のリストに結果を追加してください。あなたのコードは非常に反復的であるので、関数について学ぶことを考慮する必要があります - あなた自身を繰り返さない(DRY)。 – roganjosh

答えて

-2

無限ループだ......ここでは、このような何かを処理するための最も簡単な方法は、あなたはあなたが欲しい情報を格納しますリストを使用することです

import random 

Round = {} 
while True: 
    cnt = 1 
    ans = raw_input("Wanna Play RPS (y/n) ? ") 
    if ans == 'y' or ans == 'Y': 
     Player_Score = 0 
     Computer_Score = 0 
     while Player_Score < 5 and Computer_Score < 5: 
      Player_object = raw_input("Would you like to choose R, P, or S? ") 
      Computer_object = random.sample("RPS", 1)[0] 
      if Player_object == "R" or Player_object == "r": 
       if Computer_object == "R": 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ".You have tied with the Computer and neither of you have scored a point.") 
       elif Computer_object == "P": 
        Computer_Score += 1 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ". You have been beaten by the Computer and it has scored a point.") 
       else: 
        Player_Score += 1 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ".You have beaten the Computer and you have scored a point.") 

      if Player_object == "P" or Player_object == "p": 
       if str(Computer_object) == "R": 
        Player_Score += 1 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ".You have beaten the Computer and you have scored a point.") 
       elif str(Computer_object) == "P": 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ". You have tied with the Computer and neither of you have scored a point.") 
       else: 
        Computer_Score += 1 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ".You have been beaten by the Computer and it has scored a point.") 

      if Player_object == "S" or Player_object == "s": 
       if str(Computer_object) == "R": 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ".You have been beaten by the Computer and it has scored a point.") 
       elif str(Computer_object) == "P": 
        Computer_Score += 1 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ". You have beaten the Computer and you have scored a point.") 
       else: 
        Player_Score += 1 
        print("You have chosen " + Player_object + " and the Computer chose " + str(
        Computer_object) + ".You have tied with the Computer and neither of you have scored a point.") 

      if Computer_Score == 5 and Player_Score != 5: 
       Round[cnt] = "Computer" 
       print("The Computer has won!") 
      elif Player_Score == 5 and Computer_Score != 5: 
       Round[cnt] = "Player" 
       print("You have won and beaten the computer") 
    else: 
     break 

cnt += 1 
R = "Rock" 
r = "Rock" 
P = "Paper" 
p = "Paper" 
S = "Scissors" 
s = "Scissors" 

for i in Round: 
    print "Round ", i, "won by ", Round[i], "\n" 
+0

そして、これは「どのプレーヤーがラウンドでプレーしているのか、そして各ラウンドに勝つのか」をどのように追跡していますか? – roganjosh

0

を行きます各ラウンド。あなたのループの前に

、関連する情報を付加し、

Rounds = [] 

とあなたのループの最後で空のリストを作成します。 タプルを使用して、複数の情報をリスト内の単一のエントリとして追加できます。あなたは、ラウンドを獲得した者を追跡し、あなたのif/elseに変数にこの情報を追加し、それを追加したい場合は例えば、各プレーヤーで再生されたものを追加するには、

Rounds.append((Player_object, Computer_object)) 

を書くことができタプル。

ループが終了したら、このリストにアクセスして各ラウンドの情報にアクセスできます。リストインデックスは0から始まります。したがって、最初のラウンドの情報にアクセスするには、Rounds[0]と書いてください。タプルの各要素はインデックスでもアクセスできるため、Rounds[0][0]を使用してラウンド1のPlayer_objectにアクセスできます。

さらに詳しい情報が必要な場合は、Pythonでリストとタプルを検索してみてください。

0

各ラウンドでは、2つのものを保存したいと思いますか?選手とその動き、そして勝者。ラウンドでは、プレーヤーは一度だけ動くので、(プレーヤー、移動)のタプルがデータ構造に必要です。 1ラウンドには多くの選手が参加できるので、listを使用するのが最も適しています。したがって、各ラウンドでは、(プレイヤー、移動)タプルのリストを持つことができます。

各ラウンドで勝者を追跡したいので、ラウンドを表すタプルを作ることができます([選手と移動のリスト]、勝者)。

最後に、これらのリストが必要です。このリストをゲームループ外に定義しますが、各ループの最後にあるラウンドのデータをこのリストに追加します。 H

ほんとに役に立ちます:)

関連する問題