HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Beginner code for a Rock, Paper, Scissors game in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
paperscissorsbeginnergamerockforpythoncode

Problem

I am a beginner in Python and I wanted to see if I could get some feedback on my simple Rock, Paper, Scissors game. I do have previous experience in programming, as I started learning a few years ago but got happy feet and moved through the "basics" for a lot of different languages.

With that said, I plan on sticking with Python for a while until I have programmed a game that I find suitable (I am aiming to start small and end up maybe at a Mario clone).

My biggest issue with this program is that I find it very long-winded, and I am sure there is a way to shorten the code tremendously, I just do not know that solution yet. As well, I do plan on adding something to validate user input
but all I know so far are try and except clauses, which I am not sure if they could work.

```
# a simple game of rock, paper, scissors
import random

opponent = ["Rock", "Paper", "Scissors"]

player1Score = 0

opponentScore = 0

while (True):

if (player1Score >=3):
print("Player 1 Wins!!!!!!")
print("Type 'new game' to play again. (Or type 'exit' to exit.)")
newGame = input()
if newGame == "new game":
player1Score = 0
opponentScore = 0
continue
if newGame == "exit":
break

if (opponentScore >=3):
print("Your opponent Wins!!!!")
print("Type 'new game' to play again. (Or type 'exit' to exit.)")
newGame = input()
if newGame == "new game":
player1Score = 0
opponentScore = 0
continue
if newGame == "exit":
break

print ("Player 1's turn." + " Please type 'Rock', 'Paper', or 'Scissors' to choose your move.")

opponentMove = random.choice(opponent)
player1Move = input()

if player1Move == opponentMove:
#print (opponentMove) // for debugging
print("Your score: " + str(int(player1Score)))
print("Your opponent's score: " + str(int(opponentScore)))
print ("Tie!" + "

Solution

The code repetition could be cleaned up quickly by replacing your seven if blocks with something like:

print("Your opponent played: " + str(opponentMove) + ".")

if player1Move == opponentMove:
    print ("Tie!" + " Please go again.")
elif (player1Move == "Rock") and (opponentMove == "Paper"): # rock loses to paper
    opponentScore = opponentScore + 1
elif (player1Move == "Paper") and (opponentMove == "Rock"): # paper beats rock
    player1Score = player1Score + 1   
elif (player1Move == "Paper") and (opponentMove == "Scissors"): # paper loses to scissors
    opponentScore = opponentScore + 1
elif (player1Move == "Scissors") and (opponentMove == "Paper"): # scissors beats paper
    player1Score = player1Score + 1
elif (player1Move == "Scissors") and (opponentMove == "Rock"): # scissors loses to rock
    opponentScore = opponentScore + 1
elif (player1Move == "Rock") and (opponentMove == "Scissors"): # rock beats scissors
    player1Score = player1Score + 1
else:
    print ("Invalid choice")

print("Your score: " + str(int(player1Score)))
print("Your opponent's score: " + str(int(opponentScore)))


This eliminates all of the continue statements. Keeping with the same approach you have started, perhaps splitting into cases where player1 wins vs opponent wins is the next improvement:

if player1Move == opponentMove:
    print ("Tie!" + " Please go again.")
elif ((player1Move == "Rock") and (opponentMove == "Paper")) or
     ((player1Move == "Paper") and (opponentMove == "Scissors")) or
     ((player1Move == "Scissors") and (opponentMove == "Paper")):
    player1Score = player1Score + 1
elif ((player1Move == "Paper") and (opponentMove == "Scissors")) or
     ((player1Move == "Scissors") and (opponentMove == "Rock")) or
     ((player1Move == "Rock") and (opponentMove == "Paper")):
    opponentScore = opponentScore + 1
else:
    print ("Invalid choice")

Code Snippets

print("Your opponent played: " + str(opponentMove) + ".")

if player1Move == opponentMove:
    print ("Tie!" + " Please go again.")
elif (player1Move == "Rock") and (opponentMove == "Paper"): # rock loses to paper
    opponentScore = opponentScore + 1
elif (player1Move == "Paper") and (opponentMove == "Rock"): # paper beats rock
    player1Score = player1Score + 1   
elif (player1Move == "Paper") and (opponentMove == "Scissors"): # paper loses to scissors
    opponentScore = opponentScore + 1
elif (player1Move == "Scissors") and (opponentMove == "Paper"): # scissors beats paper
    player1Score = player1Score + 1
elif (player1Move == "Scissors") and (opponentMove == "Rock"): # scissors loses to rock
    opponentScore = opponentScore + 1
elif (player1Move == "Rock") and (opponentMove == "Scissors"): # rock beats scissors
    player1Score = player1Score + 1
else:
    print ("Invalid choice")

print("Your score: " + str(int(player1Score)))
print("Your opponent's score: " + str(int(opponentScore)))
if player1Move == opponentMove:
    print ("Tie!" + " Please go again.")
elif ((player1Move == "Rock") and (opponentMove == "Paper")) or
     ((player1Move == "Paper") and (opponentMove == "Scissors")) or
     ((player1Move == "Scissors") and (opponentMove == "Paper")):
    player1Score = player1Score + 1
elif ((player1Move == "Paper") and (opponentMove == "Scissors")) or
     ((player1Move == "Scissors") and (opponentMove == "Rock")) or
     ((player1Move == "Rock") and (opponentMove == "Paper")):
    opponentScore = opponentScore + 1
else:
    print ("Invalid choice")

Context

StackExchange Code Review Q#129621, answer score: 2

Revisions (0)

No revisions yet.