私はプログラミングの初心者です(明らかに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
'elif'sの途中で' and'から '&'に切り替えました。どうして? – DeepSpace
'randint'には両方のエンドポイントが含まれているので、約4分の1の時間で、コードはコンピュータの選択肢4を持ちます。これは' if'/'elif'条件のいずれとも一致しません。 – Blckknght
Whoa .. elifとそこにサインをしていない、気づいてくれてありがとう。あなたの質問に答えるには、私はコードを書いているうちにコンピュータが消えたので、もう一度開いたら、コードが途中で保存されました。そして、私は 'elif'の代わりに 'と'を使用することを考えていました。 'と'を書いた。 – MaraMcCrann