2016-05-18 12 views
1

Ok, ich bin ein Anfänger in der Programmierung (offensichtlich in Python) und ich habe ein Problem in meinem Code. Ich habe den Code selbst geschrieben, mit all den Dingen, die ich in Tutorials gelernt habe. Ich mache ein Stein-Schere-Papierspiel. Eigentlich funktioniert es, aber wenn ich Numpad '3' drücke, zeigt es mir den Code auf der my else-Anweisung. Kann mir jemand dabei helfen?falsche Ausgabe auf Python-Code

import random 

playerScore = 0 
cpuScore = 0 

def rpsGame(): 

    userInput = int(input("Choose (1)rock (2)paper (3)scissors: ")) 

    global playerScore 
    global cpuScore 
    cpuGuess = random.randint(1, 4) 

    if userInput == 1 and cpuGuess == 1: 
     print("A draw! rock with a rock!") 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 1 and cpuGuess == 2: 
     print("CPU scores! paper never beats rock!") 
     cpuScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 1 and cpuGuess == 3: 
     print("Player scores! rock beats sharp scissors!") 
     playerScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 2 & cpuGuess == 1: 
     print("Player! paper never beats rock!") 
     playerScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 2 & cpuGuess == 2: 
     print("A draw! paper with a paper!") 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 2 & cpuGuess == 3: 
     print("CPU scores! paper is cut with scissors!") 
     cpuScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 3 & cpuGuess == 1: 
     print("CPU scores! scissors can't cut rock!") 
     cpuScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 3 & cpuGuess == 2: 
     print("Player scores! Scissors beat paper") 
     playerScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 

    elif userInput == 3 & cpuGuess == 3: 
     print("A draw! scissors with scissors!") 
     print("Player Score:", playerScore, "CPU Score:", cpuScore)  
    else: 
     print("Error") 


while playerScore != 3 or cpuScore != 3: 
    rpsGame() 

if playerScore >= 3: 
    print("Player wins!") 

if cpuScore >= 3: 
    print("You lose! The opponent won!") 

the python shell with the outputs of my program

Es gibt einen Link in der Post, die das Bild des ‚error'.I wissen ist, habe ich Fehler in der letzten else-Anweisung geschrieben, so würde ich wissen, ob es irgendein Problem gibt. Entschuldigung für die langen "else if" -Aussagen, ich würde es ändern, wenn ich mehr Tutorials auf Python gucke und lese.Danke für: D

+1

Auf halbem Weg durch die 'elif's bist du von' und' zu '&' gewechselt. Warum? – DeepSpace

+0

'randint' enthält beide Endpunkte, also etwa ein Viertel der Zeit, Ihr Code wird den Computer Pick 4 haben, der keiner der Bedingungen 'if'/'elif' entspricht. – Blckknght

+0

Whoa.Ich habe das Elif und das & Zeichen dort nicht gesehen, danke für's Bemerken. Um Ihre Frage zu beantworten, ist, weil mein Computer ging, während ich den Code schreibe, und als ich es wieder öffnete, wurde der Code halb gespeichert, und ich dachte daran, 'und' anstelle von 'Elif' zu verwenden, also ich stattdessen schrieb 'und'. – MaraMcCrann

Antwort

-1

Schauen Sie sich die random.randint Funktion an. Leider ist es nicht dasselbe wie Entfernung oder Schneiden. Also der Grund, warum es in die else-Anweisung geht ist, dass die cpuGuess == 4.

Werfen Sie einen Blick auf PEP8, ich weiß nicht warum, aber ich schlucke nur ein wenig jedes Mal, wenn ich dieses upy-downy- hügeliger Fall.

0

Ihre CPU guess die CPU Vermutung zwischen 1 mit und 4. 4 Ersetzen mit 3.

cpuGuess = random.randint(1, 4)

werden sollten:

cpuGuess = random.randint(1, 3)

0

Diese Arbeit sollte (Ich habe einige Code, um nur zu erlauben, dass die Eingabe eine Zahl ist, die 1, 2 oder 3 ist).

import random 

playerScore = 0 
cpuScore = 0 

def rpsGame(): 
    while True: 
     try: 
      userInput = int(input("Choose (1)rock (2)paper (3)scissors: ")) 
     except ValueError: 
      print("That\'s not a number!") 
     else: 
      if 1 <= userInput < 3: 
       break 
      else: 
       print("Out of range. Try again") 

    global playerScore 
    global cpuScore 
    cpuGuess = random.randint(1, 3) 

    if userInput == 1 and cpuGuess == 1: 
     print("A draw! rock with a rock!") 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 1 and cpuGuess == 2: 
     print("CPU scores! paper never beats rock!") 
     cpuScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 1 and cpuGuess == 3: 
     print("Player scores! rock beats sharp scissors!") 
     playerScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 2 and cpuGuess == 1: 
     print("Player! paper never beats rock!") 
     playerScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 2 and cpuGuess == 2: 
     print("A draw! paper with a paper!") 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 2 and cpuGuess == 3: 
     print("CPU scores! paper is cut with scissors!") 
     cpuScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 3 and cpuGuess == 1: 
     print("CPU scores! scissors can't cut rock!") 
     cpuScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 3 and cpuGuess == 2: 
     print("Player scores! Scissors beat paper") 
     playerScore += 1 
     print("Player Score:", playerScore, "CPU Score:", cpuScore) 
    elif userInput == 3 and cpuGuess == 3: 
     print("A draw! scissors with scissors!") 
     print("Player Score:", playerScore, "CPU Score:", cpuScore)  
    else: 
     print("Error") 

while playerScore != 3 or cpuScore != 3: 
    rpsGame() 

if playerScore >= 3: 
    print("Player wins!") 

if cpuScore >= 3: 
    print("You lose! The opponent won!")