Rock, paper and scissors game in Python

Fatima Z on March 18, 2023

A young boy creating rock paper scissors game using Python programming.

Learn how to create a fun and engaging rock, paper, scissors game in Python with this step-by-step guide. Understand the rules of the game, and run it using a compiler.

Introduction


What is Python?

Python is a high-level programming language which makes the code very easy to read and understand. With no need for declaration of variables, parameters and functions, python’s codes are usually shorter compared to other programming languages. Python is a great language for beginners as it has a simple syntax and is easy to read.

Best Online Coding class for kids

What is rock, paper and scissors?
Rock, paper, scissors is a game played by using hands which can also be used as a toss when a decision on something between two or more people cannot be made. To play this game, participants say “rock, paper, scissors” and then put their hands forward forming the shape of either of the three. Making a fist represents rock, an open hand with palm facing down represents paper and two fingers (index and middle finger) extended represents scissors. In this article, we will walk through how to make a rock paper scissor game in Python.

rock, paper and scissors in Python

Step 1: Define the rules of the game

In order to create a rock paper scissor game, we should be fully familiar with the rules of the game. The rules state that rock smashes scissors, scissors cuts paper, and paper covers rock. So, rock wins over scissors, scissors win over paper and paper wins over rock. With the understanding of the rules, we can proceed to create the game.

Step 2: Writing the code

To write the code for our rock paper scissor game, we’ll use a python language compiler. Here’s an example code for a simple rock paper scissor game:

python

import random

 

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

 

user_choice = input("Choose Rock, Paper, or Scissors: ")

computer_choice = random.choice(options)

 

print("You chose: ", user_choice)

print("Computer chose: ", computer_choice)

 

if user_choice == computer_choice:

    print("It's a tie!")

elif user_choice == "Rock" and computer_choice == "Scissors":

    print("You win!")

elif user_choice == "Paper" and computer_choice == "Rock":

    print("You win!")

elif user_choice == "Scissors" and computer_choice == "Paper":

    print("You win!")

else:

    print("Computer wins!")

Step 3: Understanding the code

Let’s break down the code line by line and understand what actually happened. We start by importing the random module. This module will generate a random number or choice (if mentioned by the coder). In our code, this module will generate a random choice for the computer amongst rock, paper or scissors.
We have then defined a list of options (Rock, Paper, Scissors) that the user has to choose one from.
Next, we ask the user to input their choice using the input() function. The input that the user enters is stored in the user_choice variable. The input of the user is case sensitive in this case and it will act abnormal if the input of the user does not match any of the options we have declared above.
We use the random.choice() function to generate a random choice from the options we have declared above for the computer. The choice of the computer is stored in the computer_choice variable.
We then print out the user’s choice and the computer’s choice using the print() function. So the user can see what they choose and what the computer chooses.
We use a series of if and elif statements to determine the winner of the game. If the user and the computer make the same choice, it’s a tie. If the user chooses Rock and the computer chooses Scissors, the user wins. Similarly, if the user chooses Paper and the computer chooses Rock, or if the user chooses Scissors and the computer chooses Paper, the user wins. For the user to win, one of these conditions must come true and if none of these conditions are true, this means that the computer has won.

Best Coding Course for K12 Kids

Step 4: Running the code

To write and run python code, we need a compiler. Online compilers are also available to write and run a code. Just search “python compiler” and you will have many results. Here is an example of a compiler running the code we have written above. You should see a prompt asking you to input your choice. Once you’ve made your choice, the program will print out the computer’s choice and the winner of the game.

Running the code for rock, papers, scissors game in python

 

Conclusion

We have learned what a rock, paper and scissors game is and the rules to play this game. We also learnt how to create a simple rock paper scissor game in Python.

While this game in python may seem basic, it’s a great starting point for learning programming concepts like conditional statements, functions, and modules.

As you become more comfortable with Python, you can add more features to the game, such as keeping score, allowing inputs from the users that are not case sensitive and allowing the user to play multiple rounds.

With a little creativity, you can create a fun and engaging game that people will enjoy playing.

Coding for kids has many beneficial advantages that develop cognitive abilities, enhance communication and entrepreneurship skills, and stimulate creativity. Kids can learn how to create different games using Python.

Codingal offers Python for kids course to help your child enhance cognitive, logical, and computational skills. We provide 1:1 live interactive online coding classes with expert coding instructors for kids.

Along with lifetime access to course content and downloadable learning resources, to cater to every child’s need by providing a personalized journey.

Sign up to our newsletter

Get the latest blogs, articles, and updates delivered straight to your inbox.

Share with your friends

Try a free class