2016-05-18 4 views
1

私はプログラミングの初心者です(明らかにPythonで)。私のコードに問題があります。私はチュートリアルで学んだすべてのものを使って、コードを自分で書きました。私はロックペーパーのはさみゲームを作っています。実際には動作していますが、数字パッド '3'を押すと、私のelse文にコードが表示されます。誰かがこれで私を助けることができますか? Pythonコードの出力が間違っています

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

の絵であるポスト内のリンクがあります

「error'.Iは知っている、私は最後のelse文でエラーを書いてなかったので、どんな問題がある場合、私は知っているだろう。 長い "else if"ステートメントを残念に思っています。私はPythonについてのチュートリアルを見て、それを変更するでしょう。考慮すべき点:D

+1

'elif'sの途中で' and'から '&'に切り替えました。どうして? – DeepSpace

+0

'randint'には両方のエンドポイントが含まれているので、約4分の1の時間で、コードはコンピュータの選択肢4を持ちます。これは' if'/'elif'条件のいずれとも一致しません。 – Blckknght

+0

Whoa .. elifとそこにサインをしていない、気づいてくれてありがとう。あなたの質問に答えるには、私はコードを書いているうちにコンピュータが消えたので、もう一度開いたら、コードが途中で保存されました。そして、私は 'elif'の代わりに 'と'を使用することを考えていました。 'と'を書いた。 – MaraMcCrann

答えて

-1

random.randint関数を見てください。残念ながら、それは範囲やスライスと同じではありません。だから、それがelseステートメントに入る理由は、cpuGuess == 4です。

また、PEP8を見てください。理由はわかりませんが、私はちょっと気をつけて、丘陵ケース。

0

あなたのCPUの推測があるべき3

cpuGuess = random.randint(1, 4)

と4を交換して1と4の間でCPUの推測をしている:

cpuGuess = random.randint(1, 3)

0

これは動作するはずです(私はいくつかを追加しましたコードは、入力が1,2または3の数字のみになるようにします)。

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!") 
関連する問題