top of page
Image by Tengyart

Build a Rock Paper Scissor game with Python

Rock paper scissor is a well-played game all across the globe. What if we play this game with our computer? In this tutorial, we will learn how to build a rock paper scissor game using python programming. This could be a perfect practice for beginner python programmers. Let's start. If you are not step by step learning type of person, we got you. You can view the full source code form here.


At first, we need to import a random library, which implements pseudo-random number generators for various distributions. You don't have to install it manually, it comes in-built with Python.

from random import randint

Let's create a welcome text. It's easy, just use the print function.

print("===================================================\nWelcome to Rock Paper Scissors game !\nPlease type Rock or Paper or Scissors to choose one.\n===================================================")

Don't worry. These ==== are just for decoration.


Now let's create a list of playing options, which are Rock, Paper, and Scissors.

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

Now we need to tell our computer to pick one option between these three from our list t. For this, we will use our random function.

computer = t[randint(0,2)]

Now it's time to ask the player for his choice. For this, we will create a condition inside the while loop. First of all, we create a condition for a loop to start. Here, we are creating player = False.

player = False

Now, let's start a while loop and ask a player to enter their choice or ask to type either rock or paper or scissors.

while player == False:
    player = input("Rock, Paper, Scissors? ")

We told the player to enter their choice, now let's select the winner between the computer and the player according to their choice.


If the player and computer enter the same choice, the game is tied, which is represented by the following code.

if player == computer:
        print("Tie!")

For different choices let's break our code into conditions.


When the Player Chooses Rock

We have to use elif and else loops in this condition.


Now if the computer's choice is paper and the player's choice is rock, obviously paper covers the rock, and paper wins. This is represented by the following code.

elif player == "Rock":
        if computer == "Paper":
            print("HAHA, You lose!", computer, "covers", player)

Now if the computer choice is scissors, rock smashes the scissors and the player wins. This is represented by the following code.

else:
    print("Congrats, You win!", player, "smashes", computer)

When the Player Chooses Paper

Now the process is the same, you just need to follow the rules of the game and develop the program. Here is what it looks like.

elif player == "Paper":
        if computer == "Scissors":
            print("HAHA, You lose!", computer, "cut", player)
        else:
            print("Congrats, You win!", player, "covers", computer)

When the Player Chooses Scissors

elif player == "Scissors":
        if computer == "Rock":
            print("HAHA, You lose...", computer, "smashes", player)
        else:
            print("Congrats, You win!", player, "cut", computer)

When the Player types Invalid Option

What if the player chooses an invalid step, we need to set up a warning. We can do this by setting closing else conditions.

else:
        print("That's not a valid play. Check your spelling!")

That's all, if the player wants to play again we just need to continue asking a computer to choose options again. We can do this by setting player=False again.

player = False
    computer = t[randint(0,2)]

Now the program restarts again.


This is what a complete program looks like.


from random import randint

print("===================================================\nWelcome to Rock Paper Scissors game !\nPlease type Rock or Paper or Scissors to choose one.\n===================================================")

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


computer = t[randint(0,2)]

player = False

while player == False:
#set player to True
    player = input("Rock, Paper, Scissors? ")
    if player == computer:
        print("Tie!")
    elif player == "Rock":
        if computer == "Paper":
            print("HAHA, You lose!", computer, "covers", player)
        else:
            print("Congrats, You win!", player, "smashes", computer)
    elif player == "Paper":
        if computer == "Scissors":
            print("HAHA, You lose!", computer, "cut", player)
        else:
            print("Congrats, You win!", player, "covers", computer)
    elif player == "Scissors":
        if computer == "Rock":
            print("HAHA, You lose...", computer, "smashes", player)
        else:
            print("Congrats, You win!", player, "cut", computer)
    else:
        print("That's not a valid play. Check your spelling!")
    
    player = False
    computer = t[randint(0,2)]

Thank you for visiting, if you encounter any error in the program, you can submit your query form here. Help us by subscribing and sharing our blog. For more programming kinds of stuff, please visit our site.

25 views0 comments
bottom of page