Dark Mode On/Off

# Python Game : Rock, Paper, Scissors

Game programming is a fun way to learn any programing language. In this tutorial, we will learn how to code a simple Rock, Paper, and Scissors game.

Winning Rules in Rock, Paper, and Scissors as follows :

• Rock vs paper-> paper wins
• Rock vs scissor-> Rock wins
• paper vs scissor-> scissor wins.

## Rock, Paper, and Scissors Source Code

Here is the complete code for the project, that you can run and see how it works.

To make the above code run again and again in a loop, add the following code at the end of the `while` loop code in the above code. The code below will ask the user, after 1 game, if they want to play again, and if the user enters y, then the game will restart.

You can try this code in your laptop locally and play the game again and again.

``````ch = input("do you wish to continue(y/n)") #choice for continuing loop
if ch=="y":
player_choice=1
else:
break``````

To understand the flow, the code has been divided into the following contents-

## 1. Assign a choice to computer

``````t = ["Rock", "Paper", "Scissors"]

#random choice for computer
comp_choice = t[randint(0,2)]
``````

Import random module to use randomint() function. We assign a play option for the computer. t is a list of possible play options. randomint(0,2) will randomly generate a number from the given range each time it is called. It can give 0,1 or 2 according to our code.

comp_choice will store the value as

t[0] = "Rock"

t[1]="Paper"

t[2]="Scissor"

## 2. Take input from the player

``player_choice = input("Chose Rock, Paper, Scissors?") ``

Players will give an input of their choice

## 3. Using while loop to play multiple rounds

Take a look at the while body

``````?
while player_choice == 1:
#player choses its option
player_choice = input("Chose Rock, Paper, Scissors?")
comp_choice = t[randint(0,2)]
if player_choice == comp_choice:
print("Tie!")
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)
elif player_choice == "Rock":
if comp_choice == "Paper":
print("You lose! computer chose", comp_choice, "player chose", player_choice)
computer_win+=1
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)

else:
print("You win! player chose", player_choice, "computer chose", comp_choice)
player_win+=1
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)
elif player_choice == "Paper":
if comp_choice == "Scissors":
print("You lose! computer chose", comp_choice, "player chose", player_choice)
computer_win+=1
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)
else:
print("You win! player chose", player_choice, "computer chose", comp_choice)
player_win+=1
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)
elif player_choice == "Scissors":
if comp_choice == "Rock":
print("You lose computer chose", comp_choice, "player chose", player_choice)
computer_win+=1
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)
else:
print("You win! player chose", player_choice, "computer chose", comp_choice)
player_win+=1
print("Score")
print("computer win:",computer_win)
print("player win:",player_win)
else:
print("That's not a valid play. Check your spelling!")

print("")
ch=input("do you wish to continue(y/n)") #choice for continuing loop
if ch=="y":
player_choice=1

else:
break``````

When the loop starts, the player's choice and computer's choice are compared and whoever wins is given a point.

## 4. Display Score

``````computer_win=0
player_win=0
``````

player_win and computer_win are initialised at 0. These variables will store the scores.

## 5. Option to play again or quit

``````
ch=input("do you wish to continue(y/n)") #choice for continuing loop
if ch=="y":
player_choice=1
comp_choice = t[randint(0,2)]
else:
break
``````

Player is given the choice to continue or leave. Player's choice is stored in a variable ch. If it is 'y' then player_choice is set to 1 to continue the loop, otherwise the control executes break and comes out of the loop.

## Output-

Here we have played four rounds with the computer. After each round scores are printed for the player and computer.

Chose Rock, Paper, Scissors?Rock
Tie!
Score
computer win: 0
player win: 0

do you wish to continue(y/n)y
Chose Rock, Paper, Scissors?Paper
You lose! computer chose Scissors player chose Paper
Score
computer win: 1
player win: 0

do you wish to continue(y/n)y
Chose Rock, Paper, Scissors?Paper
Tie!
Score
computer win: 1
player win: 0

do you wish to continue(y/n)y
Chose Rock, Paper, Scissors?Scissors
You win! player chose Scissors computer chose Paper
Score
computer win: 1
player win: 1

do you wish to continue(y/n)n

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.