2017-11-21 2 views
0

Ich wurde vorgeschlagen, ein Tupel zu verwenden, um eine Methode zu erstellen, um meine Runden zu verfolgen, aber ich bin verwirrt, wie ich so etwas ausführen würde. Ich würde gerne das Objekt des Spielers und das Objekt von Computer verfolgen und wer jede Runde gewonnen hätte, aber mit dem Design meines Programms kann ich nicht verstehen, wie ich das erreichen könnte, ohne die While-Schleife 10 Mal wiederholen und den Überblick behalten zu müssen einen Gewinner auf diese WeiseTupel im Verhältnis zu unendlichen Schleifen

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

Diese Frage hat keine Frage .. – thebjorn

+0

@Joe, können Sie bitte genauer? –

+0

Was ich im Grunde frage ist, wie man ein Tupel verwendet, um viele Variablen am Ende einer Schleife zu drucken. Ich möchte drucken. "Runde X, Spieler wählte Y und Computer wählte Z" für wie viele Runden das Programm dauerte –

Antwort

0

Die Tupel unveränderlich ist, was bedeutet, dass Sie keine neue Elemente innerhalb der vorhandenen Tupel hinzufügen können, aber Sie können mit Listen tun. Deshalb können Sie die Liste Runden verwenden, die Sie bereits vorbereitet haben (und ich weiß nicht, was es tun sollte) und die Objekte dort speichern. Am Ende der Schleife können Sie über diese Liste iterieren, um sie zu extrahieren. Wenn dies nicht das ist, wonach Sie gefragt haben, seien Sie bitte genauer. Dieser Code könnte weiter optimiert werden, aber ich bin mir nicht sicher, ob Sie genug Wissen über Python und Programmierung im Allgemeinen haben, um den endgültigen Code zu verstehen - es könnte übertrieben sein.

Ich hoffe, dass dieser Code ist, was Sie gesucht haben. Vorsicht, Sie möchten dem Benutzer das Ergebnis zur Laufzeit mitteilen, damit er weiß, was passiert. Außerdem sollten Sie die Eingabe überprüfen, um Buchstaben oder Zeichenfolgen zu behandeln, die nicht gewünscht sind (z. B. Q oder ein Buchstabe, der nicht p, r oder s ist).

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.") 
Verwandte Themen