2017-11-21 14 views
0

タプルを利用してラウンドを追跡する方法を作成することを提案しましたが、そのようなことをどのように実行するのか混乱します。 Playerのオブジェクトとコンピュータのオブジェクトと各ラウンドで優勝した人を追跡したいが、私のプログラムのデザインでは、whileループを10回繰り返してトラックを維持することなく、どのように達成できるのか理解できない既存のタプル内の新しい要素を追加することはできませんが、リストにあることを行うことができることを意味し、勝者タプルと無限ループの関係

import random 
Rounds = [] 
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") 
Rounds = Rounds.append((Player_object_set, Computer_object_set)) 
R = "Rock" 
r = "Rock" 
P = "Paper" 
p = "Paper" 
S = "Scissors" 
s = "Scissors" 
print(Rounds[0][0]) 
+1

この質問に疑問はありません。 – thebjorn

+0

@Joe、もっと具体的にお聞かせください。 –

+0

私が基本的に求めているのは、ループの最後でタプルを使って多くの変数を出力する方法です。私は印刷したい。 「ラウンドX、プレーヤーはYを選択し、コンピュータはZを選択しました」というプログラムのラウンド数 –

答えて

0

タプルは不変で、この方法では、の。だからあなたは既に準備したリストラウンドを使うことができます(そして、私はそれが何をすべきかわかりません)、そこにオブジェクトを保存します。ループの最後に、そのリストを反復して抽出することができます。これがあなたが求めていたものでない場合は、より具体的にしてください。このコードはさらに最適化することができますが、最終的なコードを理解するためにPythonとプログラミングに関する知識が十分にあるかどうかは不明です。

このコードがあなたが探していたことを望みます。注意してください。実行時に結果をユーザーに伝えて、何が起きているのかを知りたい場合があります。また、望ましくない文字や文字列(Qやp、r、s以外の文字など)を処理するために、入力を確認することもできます。

import random 
Rounds = [] 
Player_Score = 0 
Computer_Score = 0 

#Tuples to contain possible outcomes 
COMPUTER_WON = ("SR", "RP", "PS") 
PLAYER_WON = ("RS", "PR", "SP") 

while Player_Score < 5 and Computer_Score < 5: 
    objects = input("Would you like to choose R, P, or S?").upper() + str(random.sample("RPS", 1)[0]) #Upper converts input to uppercase 
#objects could be tuple instead of string if you changed "+" in the line above with ",", but tuple with two strings of fixed sizes (1) wouldn't make sense 
#In that case, you should change COMPUTER_WON and PLAYER_WON to look like this (("S","R"), ("R","P"), ("P","S")) 
    Rounds.append(objects) 
    if objects in COMPUTER_WON: #if this string "objects" exists in tuple "COMPUTER_WON" i.e. if that element is one of the outcomes where computer wins: 
     Computer_Score += 1 
    elif objects in PLAYER_WON: 
     Player_Score += 1 

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") 
for i in Rounds: #For each element in list "Rounds" do the following: 
    if i in COMPUTER_WON: #if that element is one of the outcomes where computer wins: 
     print("You have chosen " + i[0] + " and the Computer chose " + i[1]+ ". You have been beaten by the Computer and it has scored a point.") 
    elif i in PLAYER_WON: 
     print("You have chosen " + i[0] + " and the Computer chose " + i[1]+ ". You have beaten the Computer and you have scored a point.") 
    else: 
     print("You have chosen " + i[0] + " and the Computer chose " + i[1]+ ". You have tied with the Computer and neither of you have scored a point.") 
関連する問題