Write Rock Paper Scissors Game Using Python

Rock Paper Scissors is a pretty well-known game. I assume that you know about it and its rule. In this blog, we are going to build this game using Python. Nothing fancy.

There must be two players to play this game. So, in this case, one will be the computer, and the other will be a real user/player. Each time, both computer and the user can choose only one word from those three (Rock, Paper, Scissors). And then, we will determine the result based on the inputs.

First, we need to import the random module so that the computer can choose a random word from the three. And we will take the player’s input where the player can put the word.

import random

words = ['rock', 'paper', 'scissors']

player = input('Choose your word from rock/paper/scissors: ').lower()
computer = words[random.randint(0, 2)]

After taking the computer and the player’s words, we will evaluate the result. In case you need, look at the following image to understand how we will do it.

Image source: Wikipedia

Before showing the result, we can also print what those two players chose.

print(f'Player: {player}. Computer: {computer}')

If they pick the same word, the result will be a tie.

In three conditions, the player can win the game. Now, we will evaluate those three conditions. Otherwise, we can determine that the computer won. Let’s see how we can write code for it.

if player == computer:
    print('Match Tie!')
elif player == 'rock' and computer == 'scissors':
    print('You won!')
elif player == 'paper' and computer == 'rock':
    print('You won!')
elif player == 'scissors' and computer == 'paper':
    print('You won!')
else:
    print('Computer won!')

Full code.

import random

words = ['rock', 'paper', 'scissors']

player = input('Choose your word from rock/paper/scissors: ').lower()
computer = words[random.randint(0, 2)]

print(f'Player: {player}. Computer: {computer}')

if player == computer:
    print('Match Tie!')
elif player == 'rock' and computer == 'scissors':
    print('You won!')
elif player == 'paper' and computer == 'rock':
    print('You won!')
elif player == 'scissors' and computer == 'paper':
    print('You won!')
else:
    print('Computer won!')

If you run it to play, it will take input from you and show you the result after evaluation.

You can add more extra features. For example, the player can define how many times the player wants to play the game. You can also store the total win count for the player and the computer for the final result. You can also write code to handle the irrelevant input from the player.

Just try to add some new features by yourself. It helps you a lot.

I hope you got the idea of how to write a program for the Rock Paper Scissors game using Python.