Ich schreibe ein Spiel von Rock, Papier und Schere in Python. Aber hier ist der Fehler:Wiederholte Bestätigung am Programmausgang
Wenn ich 8 mal gespielt habe und ich beenden will, fragt mich das Spiel auch 8 mal.
Beispiel: Ich habe 5 mal gespielt.
Das Spiel fragt mich: Do you want to exit? yes/no.
Wenn ich sage (schreiben) ja, das Spiel wieder mich fragt, ist dieser Fall 5 mal.
Ich bin ein Anfänger in Python-Programmierung, und ich verstehe wirklich nicht, warum das passiert.
Können Sie mir bitte helfen?
Ich programmiere in Windows und über die Befehlszeile, um das Programm auszuführen. (Ich weiß nicht, ob dies die korrekte Funktion des Programms beeinträchtigt.)
Hier ist der Code. Es ist noch nicht fertig:
class RPS_Game(object):
def Check(self):
P1 = raw_input("Player 1: Enter Rock, Paper or Scissors: ")
P2 = raw_input("Player 2: Enter Rock, Paper or Scissors: ")
if(str(P1.lower()) != "rock" and str(P1.lower()) != "paper" and str(P1.lower()) != "scissors"):
print "Must be Rock, Scissors or Paper, not: " + str(P1.lower())
elif(str((P2).lower()) != "rock" and str(P2.lower()) != "paper" and str(P2.lower()) != "scissors"):
print "Must be Rock, Scissors or Paper, not: " + str(P2.lower())
else:
sendDataToGame = self.Game(P1,P2)
def Game(self,P1,P2):
self.P1 = P1
self.P2 = P2
wantToExit = ""
while(True):
if(str(self.P1).lower() == str(self.P2).lower()):
print "You are in a tie!"
wantToExit = raw_input("Do you want to exit? yes/no: ")
if(wantToExit.lower() == "yes"):
break
else:
self.Check()
Call = RPS_Game() #instantiate
Call.Check() #calling Check function
Warum rufen Sie nicht 'niedriger () 'einmal, wenn Sie jede Eingabe lesen, damit Sie' P1.lower() 'nicht immer wieder schreiben müssen? – Barmar
Ich sehe nicht, wie das überhaupt funktioniert. Wenn es kein Gleichstand ist, sucht man nie nach einem Gewinner, so dass die while-Schleife für immer wiederholt wird. – Barmar