2017-11-21 8 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.")