2016-03-19 11 views
-1

Ich versuche, ein Stein Papier Schere Spiel in Python zu machen, während Sie versuchen, ein Punktesystem für Siege/Verluste zu implementieren. Wenn ich den Code ausführe, werde ich gebeten, Stein Papier oder Schere zu wählen, ich wähle meine Antwort, aber dann werde ich wieder aus irgendeinem Grund gefragt, könnte mir jemand helfen? (Ich bin so ziemlich ein Anfänger)Python-Code läuft zwei Mal

from random import randint 
def RPS(): 
    UserPts = 0 
    AIPts = 0 
    def Game(): 
     moves = ["Rock","Paper","Scissors"] 
     def genAImove(): 
      genai = moves[randint(0,2)] 
      return genai 
     genAImove() 
     AImove = genAImove() 
     def genUserMove(): 
      genu = raw_input("Choose your move ('Rock','Paper' or 'Scissors')\n") 
      return genu 
     genUserMove() 
     UserMove = genUserMove()   
     if UserMove == "Rock" and AImove == "Rock": 
      print "The AI chose rock too.\nDraw." 
      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Rock" and AImove == "Paper": 
      print "The AI chose paper.\nLoss." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Rock" and AImove == "Scissors": 
      print "The AI chose scissors.\nWin." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Paper" and AImove == "Rock": 
      print "The AI chose rock.\nWin." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Paper" and AImove == "Paper": 
      print "The AI chose paper.\nDraw." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Paper" and AImove == "Scissors": 
      print "The AI chose scissors.\nLoss." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Scissors" and AImove == "Rock": 
      print "The AI chose rock.\nLoss." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Scissors" and AImove == "Paper": 
      print "The AI chose paper.\nWin." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
     if UserMove == "Scissors" and AImove == "Scissors": 
      print "The AI chose scissors.\nDraw." 

      def cont(): 
       cnt = raw_input("Do you want to continue? (Y\N)") 
       if cnt == "Y": 
        Game() 
       elif cnt == "N": 
        exit 
       else: 
        print "Pick Y or N" 
        cont() 
      cont() 
    Game() 
RPS() 

Antwort

2

In diesen Zeilen:

genUserMove() 
UserMove = genUserMove() 

Sie erste Anruf genUserMove, die Sie fragt, und dann rufen wieder, und das Ergebnis zuordnen.

Entfernen Sie einfach die erste Zeile.

+0

Vielen Dank für die schnelle Antwort, es hat mein Problem behoben. Ich habe das überkompensiert, weil ich nicht wusste, wie globale Variablen tatsächlich funktionierten, deshalb habe ich eine Funktion in einer Funktion erstellt, bin aber auf das gleiche globale Variablenproblem gestoßen, habe es ein wenig genauer recherchiert und meine Fehler erkannt und nun funktioniert mein Spiel mit Punkten auch, obwohl mit viel mehr Code als benötigt. – dragoljub

Verwandte Themen