Implementing the Rock Paper Scissors Game in Python

-

Introduction to the Game

has been an interesting and on-the-go game since our childhood, one we could play anywhere when bored. The sport is easy. It involves two players, and every player has to decide on one in all the three options: Rock, Paper, or Scissors. Rock is expressed using a fist, Scissors with the 2 fingers opened up, and Paper with the hand opened flat.

The next are different scenarios that may occur and what they’d mean:

  • Rock vs Paper: The Paper covers the Rock. Paper wins.
  • Rock vs Scissors: The Rock breaks the Scissors. Rock wins.
  • Paper vs Scissors: The Scissors cut the Paper. Scissors wins.
  • Same signs: draw!

we are going to use our understanding of the sport, in addition to our beginner’s knowledge of Python, to code this game right into a computer program. This shall be done with the assistance of Python conditional statements: ‘if’, ‘elif’, and ‘else’, in addition to the usage of the random module, which is an in-built Python module. We’ll learn to import it and use its function to incorporate the element of randomness in our game.

Implementing the Game in Python

Now we are going to implement the sport in Python. We’ll use the concepts of Python Lists and Randomisation using the Python random module to realize our goal.

That is how this system will proceed:

This system will ask you to decide on Rock, Paper or Scissors. The pc will randomly select one in all the three selections. Based on different scenarios above, this system will determine who won the sport and can give an choice to play again.

Defining the List & Generating the ASCII Art

First, we are going to generate the ASCII Art for Rock Paper Scissors. We’ll store these inside variables named correspondingly, that are further stored inside a Python List rps_list.


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

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

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

rps_list = [rock, paper, scissors ]
Photo by Fadilah Im on Unsplash

Asking for Input from the User

The subsequent step is to get the input from the user. We’ll use the variable user_choice to store what the user chooses to play the sport with, in addition to print it out for the user to see. Notice that the variable user_choice will store the input as a string. This key point shall be useful to recollect once we use conditionals to match each the user’s and the pc’s selections in our article ahead.

user_choice = input("What do you select? Type 'rock' for Rock, 'scissors' for Scissors and 'paper' for Paper")
print(f"User chooses {user_choice}")

Computer’s Random Selection

Once the user has decided their alternative, next we are going to make the pc make a random alternative. We’ll use the random module for this purpose. You possibly can check more about this through the next link:

random — Generate pseudo-random numbers

The random module’s alternative() function allows us to randomly choose from a given Python list that has been given as a parameter to it. We’ll store this random alternative within the variable computer_choice and print it out.

import random
computer_choice = random.alternative(rps_list)
print(f"Computer chooses {computer_choice}")

Furthermore, you too can check this text, that tells you the best way to include randomisation in our code using Python’s random module. It includes a simple explanation of different functions with easy-to-understand examples:

Tips on how to Implement Randomization with the Python Random Module

Scenarios using Conditionals

Now we are going to define all different scenarios that we mentioned to start with in code form. We’ll use if, elif, and else, that are Python’s conditional statements, for this purpose.

if computer_choice == rock and user_choice == 'scissors':
    print("You lose")
elif computer_choice == rock and user_choice == 'paper':
    print("You win")
elif computer_choice == rock and user_choice == "rock":
    print("Draw")
elif computer_choice == paper and user_choice == 'paper':
    print("Draw")
elif computer_choice == paper and user_choice == 'scissors':
    print("You win")
elif computer_choice == paper and user_choice == "rock":
    print("You lose")
elif computer_choice == scissors and user_choice == 'scissors':
    print("Draw")
elif computer_choice == scissors and user_choice == "rock":
    print("You win")
elif computer_choice == scissors and user_choice == 'paper':
    print("You lose")
else:
    print("Error")

As might be seen from the code above, we’ve got utilized every scenario, comparing the pc’s alternative with the user’s alternative that had been stored as a string as might be understood from the inverted comma, after which printed out the outcomes, whether the user wins, or the pc wins or it has resulted in a draw between them each.

Conclusion

The above program is a straightforward Python code, which is simple to know and provides an introduction to Python’s conditionals and the usage of the random module, specifically its alternative function.

Although there are plenty of ways by which the scenarios might have been coded, the above was an explicit and beginner-friendly code involving the if, elif and else conditionals. Can you consider some other way this game might have been coded?

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x