Skip to content

Rock Paper Scissors Game

Abstract

Rock, Paper, Scissors is a hand game played between two people. The game is played by two people using their hands. The players count to three in unison and simultaneously “throw” one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the hand signals thrown by the players. The rules of the game are:

  • Rock wins against scissors.
  • Scissors win against paper.
  • Paper wins against rock.
  • If both players throw the same hand signal, there is no winner and the game is played again.

We are going to make a simple rock paper scissors game using python. This is a beginner level project. You can find the code for this project on my GitHub. In this project, we will use the random module to generate random numbers and then we will use if-else statements to check the user input and computer generated number and then we will decide the winner. You are against the computer in this game. The computer will randomly choose between rock, paper and scissors. You will be asked to choose between rock, paper and scissors. The computer will then check your choice against its choice and decide the winner. The game will continue until you decide to quit.

Prerequisites

  • Python 3.6 or above
  • A code editor or IDE

Getting Started

Create a Project

  1. Create a new folder and name it rockpaperscissorsgamerockpaperscissorsgame.
  2. Open the folder in your code editor.
  3. Create a new file and name it rockpaperscissors.pyrockpaperscissors.py.
  4. Copy the code given at the beginning of this article and paste it in the rockpaperscissors.pyrockpaperscissors.py file.

Write the Code

  1. Copy and paste the following code in the rockpaperscissors.pyrockpaperscissors.py file.
⚙️ Rock Paper Scissors
Rock Paper Scissors
# Rock, Paper, Scissors Game
 
# Import Libraries
import random
 
# Creating a list of options
options = ["ROCK", "PAPER", "SCISSORS"]
 
# Creating a function to play the game
def play():
    # Getting the user's choice
    user_choice = input("Choose Rock, Paper or Scissors: ").upper()
    
    # Getting the computer's choice
    computer_choice = random.choice(options)
    
    # Checking if the user's choice is valid
    while user_choice not in options:
        user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
    
    # Checking the user's choice against the computer's choice    
    if user_choice.upper() == computer_choice.upper():
        print(f"Computer chose {computer_choice}. It's a tie!")
    elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
        print(f"Computer chose {computer_choice}. You win!")
    elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
        print(f"Computer chose {computer_choice}. You win!")
    elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
        print(f"Computer chose {computer_choice}. You win!")
    else:
        print(f"Computer chose {computer_choice}. You lose!")
        
        
# Playing the game
while True:
    play()
    play_again = input("Do you want to play again? (y/n): ")
    if play_again.lower() != "y":
        break
    
print("Thanks for playing!") 
Rock Paper Scissors
# Rock, Paper, Scissors Game
 
# Import Libraries
import random
 
# Creating a list of options
options = ["ROCK", "PAPER", "SCISSORS"]
 
# Creating a function to play the game
def play():
    # Getting the user's choice
    user_choice = input("Choose Rock, Paper or Scissors: ").upper()
    
    # Getting the computer's choice
    computer_choice = random.choice(options)
    
    # Checking if the user's choice is valid
    while user_choice not in options:
        user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
    
    # Checking the user's choice against the computer's choice    
    if user_choice.upper() == computer_choice.upper():
        print(f"Computer chose {computer_choice}. It's a tie!")
    elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
        print(f"Computer chose {computer_choice}. You win!")
    elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
        print(f"Computer chose {computer_choice}. You win!")
    elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
        print(f"Computer chose {computer_choice}. You win!")
    else:
        print(f"Computer chose {computer_choice}. You lose!")
        
        
# Playing the game
while True:
    play()
    play_again = input("Do you want to play again? (y/n): ")
    if play_again.lower() != "y":
        break
    
print("Thanks for playing!") 
  1. Save the File.
  2. Open the terminal and navigate to the rockpaperscissorsgamerockpaperscissorsgame folder.
command
C:\Users\username\PythonCentralHub\projects\beginners\rockpaperscissorsgame> python rockpaperscissors.py
Choose Rock, Paper or Scissors: Rock
Computer chose SCISSORS. You win!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: paper
Computer chose PAPER. It's a tie!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: scissors
Computer chose SCISSORS. It's a tie!
Do you want to play again? (y/n): n
Thanks for playing!
command
C:\Users\username\PythonCentralHub\projects\beginners\rockpaperscissorsgame> python rockpaperscissors.py
Choose Rock, Paper or Scissors: Rock
Computer chose SCISSORS. You win!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: paper
Computer chose PAPER. It's a tie!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: scissors
Computer chose SCISSORS. It's a tie!
Do you want to play again? (y/n): n
Thanks for playing!
  1. You can see that the game is working as expected. You can play the game as many times as you want. You can also quit the game whenever you want.

Explanation

  1. We first import the randomrandom module. We will use this module to generate random numbers.
rockpaperscissors.py
import random
rockpaperscissors.py
import random
  1. We create a list of options. The computer will randomly choose from this list.
rockpaperscissors.py
options = ["ROCK", "PAPER", "SCISSORS"]
rockpaperscissors.py
options = ["ROCK", "PAPER", "SCISSORS"]
  1. We create a function to play the game. This function will be called whenever we want to play the game.
rockpaperscissors.py
def play():
rockpaperscissors.py
def play():
  1. We get the user’s choice. We use the input()input() function to get the user’s choice. We convert the user’s choice to uppercase using the upper()upper() function. We store the user’s choice in the user_choiceuser_choice variable.
rockpaperscissors.py
user_choice = input("Choose Rock, Paper or Scissors: ").upper()
rockpaperscissors.py
user_choice = input("Choose Rock, Paper or Scissors: ").upper()
  1. We get the computer’s choice. We use the random.choice()random.choice() function to get the computer’s choice. We pass the optionsoptions list as an argument to the random.choice()random.choice() function. We store the computer’s choice in the computer_choicecomputer_choice variable.
rockpaperscissors.py
computer_choice = random.choice(options)
rockpaperscissors.py
computer_choice = random.choice(options)
  1. We check if the user’s choice is valid. We use the whilewhile loop to check if the user’s choice is valid. We check if the user’s choice is in the optionsoptions list. If the user’s choice is not in the optionsoptions list, we ask the user to enter a valid choice. We convert the user’s choice to uppercase using the upper()upper() function. We store the user’s choice in the user_choiceuser_choice variable.
rockpaperscissors.py
while user_choice not in options:
    user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
rockpaperscissors.py
while user_choice not in options:
    user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
  1. We check the user’s choice against the computer’s choice. We use the if-elif-elseif-elif-else statements to check the user’s choice against the computer’s choice. We convert the user’s choice to uppercase using the upper()upper() function. We convert the computer’s choice to uppercase using the upper()upper() function. If the user’s choice is equal to the computer’s choice, we print that it’s a tie. If the user’s choice is rock and the computer’s choice is scissors, we print that the user wins. If the user’s choice is paper and the computer’s choice is rock, we print that the user wins. If the user’s choice is scissors and the computer’s choice is paper, we print that the user wins. If the user’s choice is not equal to the computer’s choice, we print that the user loses.
rockpaperscissors.py
if user_choice.upper() == computer_choice.upper():
    print(f"Computer chose {computer_choice}. It's a tie!")
elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
    print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
    print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
    print(f"Computer chose {computer_choice}. You win!")
else:
    print(f"Computer chose {computer_choice}. You lose!")
rockpaperscissors.py
if user_choice.upper() == computer_choice.upper():
    print(f"Computer chose {computer_choice}. It's a tie!")
elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
    print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
    print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
    print(f"Computer chose {computer_choice}. You win!")
else:
    print(f"Computer chose {computer_choice}. You lose!")
  1. We play the game. We use the whilewhile loop to play the game. We call the play()play() function to play the game. We ask the user if they want to play again. If the user enters yy, we play the game again. If the user enters nn, we break out of the whilewhile loop.
rockpaperscissors.py
while True:
    play()
    play_again = input("Do you want to play again? (y/n): ")
    if play_again.lower() != "y":
        break
rockpaperscissors.py
while True:
    play()
    play_again = input("Do you want to play again? (y/n): ")
    if play_again.lower() != "y":
        break
  1. We print a message to thank the user for playing the game.
rockpaperscissors.py
print("Thanks for playing!")
rockpaperscissors.py
print("Thanks for playing!")

Next Steps

Congratulations on completing this project. You can now move on to the next project where we will make a simple calculator using python. You can find the code for this project on my GitHub. If you like this article, please share it with your friends and colleagues. If you have any questions or suggestions, please let me know in the comments below.

There are some suggestions:

  • Add a score counter to keep track of the score.
  • Add a timer to limit the time for each round.
  • Add a GUI to make the game more interactive.
  • Add more options to make the game more interesting.
  • Add a database to store the scores.
  • Add a leaderboard to display the top scores.
  • Add a multiplayer mode to play with friends.
  • Add a chat feature to chat with friends while playing the game.
  • Add a voice assistant to make the game more interactive.
  • Add a virtual assistant to play the game with you.

Conclusion

In this article, we learned how to make a simple rock paper scissors game using python. This is a beginner level project. You can find the code for this project on my GitHub. In this project, we used the random module to generate random numbers and then we used if-else statements to check the user input and computer generated number and then we decided the winner. You can create more projects using python. You can find more projects on Python Central Hub. If you have any questions or suggestions, please let me know in the comments below.

Was this page helpful?

Let us know how we did