Rock Paper Scissors in Python

salmanakhtar57

Salman Akhtar

Posted on February 9, 2023

Rock Paper Scissors in Python

A Step-by-Step Guide to implement ‘Rock Paper Scissors’ in Python

Image Pexels.com by [Pavel Danilyuk](https://www.pexels.com/@pavel-danlyuk/)

Rock Paper Scissors is a fun game, played between two players. Each player picks one option either rock, paper, or scissors. And the one who picks the right option wins. The winner is determined by three simple rules.

Here are the rules of the game:

  • Rock beats Scissors

  • Paper beats Rock

  • Scissors beat paper.

So according to the rules, if you choose rock, and the other player chooses scissors, or you choose paper, and the other player chooses rock, or you choose scissors, and the other player chooses paper, then you will win the game.

But if the opposite is the case then the other player wins. The match will be drawn if both choose the same option.

Let's implement this game in Python

Prequesities

This game can be implemented in any programming language but here we will implement it in Python.

To understand how this game works, you need to know some basic understanding of Python including if-else, for loops, arrays, and return statements.

You also need to know how to define and call functions in Python.

If you are a beginner in Python it’s a great way to practice your Python Skills.

Implementation

Now, let's start implementing our game step by step in Python.

Because we play this game against the computer, we will start by importing the random function in Python. This function allowed the computer to pick any random option on its own.

import random
Enter fullscreen mode Exit fullscreen mode

Now we create a function get_computer_choice(). In this function, we declare the variable choices which will give the computer three choices and it can select one choice randomly.

def get_computer_choice(): 
    choices = [‘rock’, ‘paper’, ‘scissors’] 
    return random.choice(choices)
Enter fullscreen mode Exit fullscreen mode

Create another function play_round(). In this function, we start by converting player_selection and computer_selection to lowercase. This way we can write either lower or upper case and avoid case sensitivity while playing the game.

After that, we compare a player's choice with a computer's choice. If let says both give the same choices then the game is drawn. Otherwise, the one who picks the right choice will be the winner. We are using the if-else statement to determine the winner and the loser.

When the match is drawn, it returns 0, if the player wins it returns 1 and returns -1 if the player loses. This helps to determine the score of the player and the computer in the next function game().

def play_round(player_selection, computer_selection):

    player_selection = player_selection.lower()
    computer_selection = computer_selection.lower() 

    if (player_selection == computer_selection):
        print(f"Both selected {player_selection}. It's a draw.")
        return 0
    elif (player_selection == "rock" and computer_selection == "scissors" or 
         player_selection == "paper" and computer_selection == "rock" or 
         player_selection == "scissors" and computer_selection == "paper"):
        print(f"{player_selection} beats {computer_selection} \nCongrats You win!" )
        return 1
    else:
        print(f"{computer_selection} beats {player_selection} \nYou lose!")
        return -1
Enter fullscreen mode Exit fullscreen mode

Lastly, we will create a function game(). In this function, we will declare two variables player_score and computer_score and initialize them to zero.

Basically, we set the game so that it will allow both the player and the computer to play 5 games with the help of for loop. After the 5 games, the winner will be displayed.

Within the for loop, the player will pick his choice while the computer selects any choice randomly.

Next, it will check the result with the help of a play_round() function. If the result is 1 the player score is incremented or if it's -1 the computer score is incremented. In the case of 0, no score is incremented on either side.

It compares the player_score and the computer_score and decides the winner based on the result.

In the end, we call the function game() that will show the message of who wins the game or if it's drawn. Calling the game() function will help the function to run and play the game.

def game():
    player_score = 0
    computer_score = 0

    for i in range(5):
        player_selection = input("\nPick your choice, 'Rock', 'Paper' or 'Scissors': ")
        computer_selection = get_computer_choice()

        result = play_round(player_selection, computer_selection)

        if result == 1:
            player_score += 1
        elif result == -1:
            computer_score += 1

    if player_score > computer_score:
        print(f"\nFinal score: {player_score} - {computer_score}. You won the game!")
    elif player_score < computer_score:
        print(f"\nFinal score: {player_score} - {computer_score}. You loss the game!")
    else:
        print(f"\nFinal score: {player_score} - {computer_score}. The game is draw!")

game()
Enter fullscreen mode Exit fullscreen mode

Below is the output of the game that we have just implemented:

As you see from the image when both players select the same choice, the game is drawn and if we choose rock against paper we lose, or choose paper against rock we win.

The final score is displayed at the end of the game saying that we lost by 1–2 from the computer.

So this is all we did. This article explains how we implemented the game ‘Rock, Paper and Scissors’ in Python.

You can check out my GitHub repository for the implementation of this code.

If you like this article, follow me for more such content. Always feel free to reach out to me on Twitter.

💖 💪 🙅 🚩
salmanakhtar57
Salman Akhtar

Posted on February 9, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related