How to code a simple rock, paper, scissors game on Python

How to code a simple rock, paper, scissors game on Python

Hi everyone,

Welcome to my blog. In this post, I will show you how to code your first game on python.

A simple way to improve your coding skills is by trying out new projects. There are many projects that could help you get into python, and one of the best and most straightforward projects out there is the rock, paper, scissors game.

How to get started?

  1. Install Python
  2. Install a code editor such as VS Code, PyCharm, IntelliJ, Jupyter Notebook (I use Jupypter Notebook).
  3. Get familiar with Python such as variables, Data Types, lists, dictionaries and so on.
  4. Start working on projects to improve your python programming skills.

1. Install Python

To install python please click on the following link and download python for your operating system.
python.org/downloads

2. Install a code editor such as Jupyter Notebook.

The simplest way to get started with Jupyter Notebook is to download Anaconda Navigator.
docs.anaconda.com/anaconda/install

3. Get familiar with Python

Python is an interpreted, object-oriented programming language. It is often used to build websites, softwares, automated tasks such as bots, conduct data analysis and is widely used in Artificial Intelligence. To learn the basics of Python, please click on the link below.
python.land/introduction-to-python

4. Start working on projects (Rock, Paper, Scissors).

After familiarising yourself with a code editor and the basics of Python, we can start building our first project.

Line 1:

import random

The random module is a built-in module to generate the pseudo-random variables. It can be used to perform some action randomly such as getting a random number, selecting a random element from a list, shuffling elements randomly, etc.

Line 2:

user = input("Enter a choice (rock, paper, scissors): ")

This will prompt the user to enter a selection from the given list.

Line 3 & 4:

possible = ["rock", "paper", "scissors"]
computer = random.choice(possible)

This allows the computer to randomly choose from one of the 3 options (rock, paper, scissors).

Line 5:

print(f"\nYou chose {user}, computer chose {computer}.\n")

This line will print the choices of the user and the computer.

Now that the computer and the user has picked their options, we need to find a way to determine who is a winner. To do that, we will be using if, elif and else.

Line 6+:

if user == computer: 
    print(f"Both players selected {user}. It's a tie!")
elif user == "rock":
    if computer == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose!")
elif user == "paper":
    if computer == "rock":
        print("Paper covers rock, You win!")
    else:
        print("Scissors cuts paper! You lose!")
elif user == "scissors":
    if computer == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose!")

This block of code will go through the possibilities to see who the winner is and print the outcome:

  • Rock smashes scissors
  • Paper covers rock
  • Scissors cuts paper

All the code combined:

import random

user = input("Enter a choice (rock, paper, scissors): ")
possible = ["rock", "paper", "scissors"]
computer = random.choice(possible)
print(f"\nYou chose {user}, computer chose {computer}.\n")

if user == computer:
    print(f"Both players selected {user}. It's a tie!")
elif user == "rock":
    if computer == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose!")
elif user == "paper":
    if computer == "rock":
        print("Paper covers rock, You win!")
    else:
        print("Scissors cuts paper! You lose!")
elif user == "scissors":
    if computer == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose!")

Testing the game

When running the code, this is what you will see. The user will need to enter their choice: Screenshot 2022-06-21 at 12.06.12.png

In this example, I chose paper and lost. The computer chose scissors and as we know Scissors cuts paper Screenshot 2022-06-21 at 12.06.44.png

In my second attempt, I chose rock and won!!. The computer chose scissors, as the rules says Rock smashes scissors

Screenshot 2022-06-21 at 12.06.35.png

Congratulations!

In this tutorial, you learned how to code your first game using Python. You can see how easy and straightforward it is to code the game from scratch.

I hope you enjoyed this tutorial. Please make sure to see my future posts for more contents :).

Click on the link below to check my video on YouTube on how to code the game.
youtube.com/watch?v=Za3edTo9-uw&t=233s

Did you find this article valuable?

Support Hussain Hussaini by becoming a sponsor. Any amount is appreciated!