Rock, Paper and Scissors Game in Python

Rock, Paper and Scissors Game in Python

Hello Everyone!!, How are you all doing? Today we will be creating a game called "Rock Paper Scissors" using Python. In this article, I will be showing you how can we create a "Rock Paper Scissors" game using Python (again this is not a GUI game, we will create a GUI version later on). So, without wasting any time let's start coding

NOTE:- I am going ahead by assuming that you know what a game of rock paper scissors is, in case you don't know about it and you want to know about the game and its rules you can read about it here or I have also written down the rules below in the coding section. Okay, now let's start...

Writing Program for single Game

We are going to start by importing the random module which will be used to generate a random computer decision so that we have no idea what the computer might choose among rock paper or scissor.

import random

Now, what we will do is we'll store the rock, paper and scissor ASCII art into respective variables and then create a list of those 3 variables this will come in handy when we will display the choice of the user and while selecting a random choice for computer.

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

choices = [rock, paper, scissors]

(OPTIONAL) display a message on top of the screen "ROCK PAPER SCISSORS"

print("Rock Paper Scissors")

Now, it's time to ask the user their choice and for the choice, we will ask the user to input a number from 0, 1 or 2, so that we can accordingly display the respective art from the choices list we created above

We are taking input from 0 because in python the values inside of the array are mapped to some index value which starts from 0 and end at n-1 (n being the size of the list). Example:- If new_list = [12, 13, 14], then new_list[0] = 12, new_list[1] = 13 and new_list[2] = 14.

We will also have to check the number entered by the user, if the user enters a number greater than 2 or less than 0 then we will display a message "Invalid Choice. You lost" otherwise will continue the game and store the user's choice in a variable and its instead of the number will store the art stored at that index inside choices list and also display it to the screen.

num = int(input("Enter your choice (0 for rock, 1 for paper and 2 for scissors): "))
if num > 2 or num < 0:
        print("Inavlid choice.\nYou lost")
        break
user_choice = choices[num]
print(user_choice)

The next and the last part of the code is to make a random choice for the computer, display it on the screen and then according to the rules of the game select the winner and display it on the screen. Selecting the random choice can be done using the choice method of the random module which lets us choose any random element from a list. And for selecting a winner we will use a bunch of if-else loop according to the rules of "Rock Paper Scissors".

Rules of Rock Paper Scissor:-

  1. Rock vs Paper: Paper wins
  2. Paper vs Scissor: Scissor wins
  3. Scissor vs Rock: Rock wins
print("Computer chose:")
if user_choice == rock:
    comp_choice = random.choice(choices)
    print(comp_choice)
    if comp_choice == rock:
        print("Its a draw")
    elif comp_choice == paper:
        print("You lost")
    else:
        print("You won!!")
elif user_choice == paper:
    comp_choice = random.choice(choices)
    print(comp_choice)
    if comp_choice == rock:
        print("You won!!")
    elif comp_choice == paper:
        print("Its a draw")
    else:
        print("You lost")
else:
    comp_choice = random.choice(choices)
    print(comp_choice)
    if comp_choice == rock:
       print("You lost")
    elif comp_choice == paper:
        print("You Won!!")
    else:
        print("Its a draw")

Logic for multiple games without exiting

We just need to add an infinite loop over the code where we ask the user for input, select a random choice for the computer and display a winner accordingly, and we will also be asking for the user input at the end of every loop, if they want to continue playing or not "y" will be for "yes, I want to play again" and "n" will be for "No" and according to the user input, we will continue the game or exit out of the loop using a break statement.

while True:  // new line added for multiple games
    num = int(input("Enter your choice (0 for rock, 1 for paper and 2 for scissors): "))
    if num > 2 or num < 0:
        print("Inavlid choice. You lost")
        break
    user_choice = choices[num]
    print(user_choice)
    print("Computer chose:")
    if user_choice == rock:
        comp_choice = random.choice(choices)
        print(comp_choice)
        if comp_choice == rock:
            print("Its a draw")
        elif comp_choice == paper:
            print("You lost")
        else:
            print("You won!!")
    elif user_choice == paper:
        comp_choice = random.choice(choices)
        print(comp_choice)
        if comp_choice == rock:
            print("You won!!")
        elif comp_choice == paper:
            print("Its a draw")
        else:
            print("You lost")
    else:
        comp_choice = random.choice(choices)
        print(comp_choice)
        if comp_choice == rock:
            print("You lost")
        elif comp_choice == paper:
            print("You Won!!")
        else:
            print("Its a draw")

    // new lines for multiple games
    cont = input("Do you want to continue playing (Y/N)").lower()
    if cont == "n":
        print("Okay Goodbye!!")
        break
    else:
        continue

COMPLETE CODE

import random
rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

choices = [rock, paper, scissors]

print("Rock, Paper and Scissors ")
while True:
    print("What do you choose?")
    num = int(input("Enter your choice (0 for rock, 1 for paper and 2 for scissors): "))
    if num > 2 or num < 0:
        print("Inavlid choice.\nYou lost")
        break
    user_choice = choices[num]
    print(user_choice)
    print("Computer chose:")
    if user_choice == rock:
        comp_choice = random.choice(choices)
        print(comp_choice)
        if comp_choice == rock:
            print("Its a draw")
        elif comp_choice == paper:
            print("You lost")
        else:
            print("You won!!")
    elif user_choice == paper:
        comp_choice = random.choice(choices)
        print(comp_choice)
        if comp_choice == rock:
            print("You won!!")
        elif comp_choice == paper:
            print("Its a draw")
        else:
            print("You lost")
    else:
        comp_choice = random.choice(choices)
        print(comp_choice)
        if comp_choice == rock:
            print("You lost")
        elif comp_choice == paper:
            print("You Won!!")
        else:
            print("Its a draw")

    cont = input("Do you want to continue playing (Y/N)").lower()
    if cont == "n":
        print("Okay Goodbye!!")
        break
    else:
        continue

Conclusion

I hope this was a fun small project/game to build and practice your python skills. I would like you to update this game in some different way as you like, I will give you an idea for that you can also the user if they want to play against a computer or if there are two players playing the game and then accordingly change the logic of the code.

So, that's it. Thank you for reading the article and best of luck for changing this game and creating your own version.