2017-11-19 3 views
0

So versuche ich zu verfolgen, was in einer Runde von jedem Spieler gespielt wird und wer jede Runde in meinem R/P/S Programm gewinnt. Gibt es eine Möglichkeit, dies in einer möglicherweise unendlich wiederholten Schleife zu tun? Hier ist der CodeWeise, um Runden während einer while Schleife zu behalten

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

Erstellen Sie einfach eine Liste, bevor Ihre 'while' Schleife startet und fügen Sie die Ergebnisse für jedes Ergebnis an diese Liste an. Sie sollten auch über Funktionen nachdenken, da Ihr Code sehr repetitiv ist - Wiederholen Sie sich nicht (DRY). – roganjosh

Antwort

-2

Unendlich-Schleife ...... hier gehen Sie

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

Und wie kann man "verfolgen, was jeder Spieler in einer Runde spielt und wer jede Runde gewinnt"? – roganjosh

0

Der einfachste Weg, um etwas zu behandeln, wie das ist, eine Liste zu verwenden, wo Sie die Informationen speichern, würden Sie wollen in jede Runde.

Bevor Sie Schleife, erstellen Sie eine leere Liste

Rounds = [] 

und am Ende der Schleife, die relevanten Informationen anfügen. Sie können ein Tupel verwenden, um mehrere Informationen als einzelnen Eintrag in der Liste hinzuzufügen. Zum Beispiel hinzufügen, was von jedem Spieler gespielt wird, könnte man

Rounds.append((Player_object, Computer_object)) 

schreiben Wenn Sie den Überblick behalten möchten, die die Runde gewonnen, fügen Sie diese Informationen in einer Variablen in Ihrem if/else und fügen Sie ihn in die Tupel.

Sobald Ihre Schleife vorbei ist, können Sie auf die Informationen jeder Runde zugreifen, indem Sie auf diese Liste zugreifen. Listenindizes beginnen bei 0, um auf die Informationen der ersten Runde zuzugreifen, würden Sie Rounds[0] schreiben. Auf jedes Element in einem Tupel kann auch per Index zugegriffen werden, so dass der Zugriff auf Player_object von Runde 1 unter Verwendung von Rounds[0][0] möglich ist.

Wenn Sie weitere Informationen benötigen, versuchen Sie in Python nach Listen und Tupeln zu suchen.

0

Für jede Runde möchten Sie zwei Dinge speichern, oder? Die Spieler und ihre Züge und ihr Gewinner. In einer Runde bewegt sich ein Spieler nur einmal, also würden wir ein Tupel von (Spieler, Bewegung) in unserer Datenstruktur haben wollen. Es könnte viele Spieler geben, die an einer Runde teilnehmen, also wäre die Verwendung einer list am besten geeignet. Daher können Sie in jeder Runde eine Liste von Tupeln von (Spieler, bewegen) haben.

Da Sie mit jeder Runde auch den Gewinner verfolgen möchten, können Sie ein Tupel erstellen, um eine Runde darzustellen: ([Liste der Spieler und Züge], Gewinner).

Schließlich möchten Sie eine Liste von diesen! Definieren Sie diese Liste außerhalb der Spielschleife, fügen Sie jedoch die Daten einer Runde am Ende jeder Schleife an diese Liste an. H

Hoffe das hilft :)

Verwandte Themen