Skip to main content

Rock Paper Scissors Game in Python

Rock-Paper-Scissors is a popular game played across the world. In this article, the game is implemented using a simple AI and a command line interface in Python programming language.


The code is built and executed using the following tools,

  • Operating System - Windows 10
  • Interpreter - Python 3.10
  • Editor - Visual Studio Code

The game play is very simple, two players pick one of rock, paper or scissors. If they both choose the same value, then the round is drawn. Otherwise, the winner is decided based on the following logic,

  • Rock beats Scissors
  • Scissors beat Paper
  • Paper beats Rock

To simulate the game play, the player (or the user) plays against an AI which chooses an option before the player decides. The winner is decided after the player chooses an option, the scores are updated as per the winner and gets displayed at the end of each round. To simulate many rounds, the code is run in an infinite loop until the user wishes to exit the game. The code is as follows,

import random

validInput = ['R', 'P', 'S']
rps = {
        'R': {'name': 'Rock', 'beats': 'S'},
        'P': {'name': 'Paper', 'beats': 'R'},
        'S': {'name': 'Scissors', 'beats': 'P'}
score = {'ai': 0, 'user': 0}


while True:    
    aiOption = random.choice(validInput)
    userOption = input("Rock [R]/Paper [P]/Scissors [S]/Exit [E]: ").upper()
    if userOption == 'E':
    if userOption not in validInput:
        print('error: Invalid input')
    print(f"User - {rps[userOption]['name']}, AI - {rps[aiOption]['name']}")
    if rps[userOption]['beats'] == aiOption:        
        print(f"{rps[userOption]['name']} beat {rps[aiOption]['name']}")
        score['user'] += 1
    elif rps[aiOption]['beats'] == userOption:
        print(f"{rps[aiOption]['name']} beat {rps[userOption]['name']}")
        score['ai'] += 1

    print(f"Score: User - {score['user']}, AI - {score['ai']}")

The list of valid inputs are maintained in an array named validInput. The game logic is enclosed in a dictionary of dictionaries named rps and scores of both AI and player are kept in another dictionary named score.

The code starts with initializing a seed for the random library and jumps right into the infinite loop. For each iteration, the AI picks a random value from the list of valid inputs and the code waits for the player to enter an input. The input is validated later, and an invalid input will put back the user to enter an option again. The game then decides the winner or draw based on the input from the player and the AI, increases score if there is a clear winner and displays the score at the end of each round. The command line output is as follows,

> python.exe
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: r
User - Rock, AI - Rock
Score: User - 0, AI - 0
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: p
User - Paper, AI - Scissors
Scissors beat Paper
Score: User - 0, AI - 1
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: s
User - Scissors, AI - Paper
Scissors beat Paper
Score: User - 1, AI - 1
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: r
User - Rock, AI - Paper
Paper beat Rock
Score: User - 1, AI - 2
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: p
User - Paper, AI - Rock
Paper beat Rock
Score: User - 2, AI - 2
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: s
User - Scissors, AI - Scissors
Score: User - 2, AI - 2
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: r
User - Rock, AI - Rock
Score: User - 2, AI - 2
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: p
User - Paper, AI - Paper
Score: User - 2, AI - 2
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: s
User - Scissors, AI - Rock
Rock beat Scissors
Score: User - 2, AI - 3
Rock [R]/Paper [P]/Scissors [S]/Exit [E]: e

Thanks for reading. Please leave suggestions/comments (if any).


Popular posts from this blog

BMI Calculator using Python and Tkinter

Body Mass Index can be calculated using a simple formula kg/m 2 , where the weight is represented in kilograms (kg) and the height is represented in metres (m). The article presents code to create a simple GUI to calculate BMI based on user input values. The GUI is implemented using tkinter and tkinter.ttk libraries in Python language.

Tic-tac-toe game using Python and tkinter

Tic-tac-toe is a popular two player game where each player tries to occupy an empty slot in a 3x3 board until one of them wins or the entire board is filled without any winner. The winner has to occupy three continuous cells in any direction, including the two diagonals. In this article, a version of the tic-tac-toe game is coded using Python and tkinter library.

Using API in Python is a light weight website made by Alexander Kojevnikov , used to format source code from various programming languages into formatted HTML snippets which can be added to blogs, articles, and other web pages as needed. I personally use the API to format the source code snippets in this blog, and it has greatly reduced the time taken to complete an article, much thanks to the creator. In this article, an automated way of using the API through Python and a simple HTML document is created with formatted code snippets.