Implementing the Rock Paper Scissors game in Python

An introduction to this game
has been a fun and on-the-go game since our childhood, one we played wherever we were bored. The game is simple. It involves two players, and each player must choose one of 3 options: rock, paper, or scarf. Rock is portrayed using a fist, scissors with two fingers spread outward, and paper with an open hand.
The following are the various possible scenarios and what they will mean:
- Rock vs Paper: Paper covers the rock. The papers won.
- Rock vs Scissors: Rock breaks scissors. The Rock wins.
- Paper VS Scissors: Scissors Cut paper. Scissors wins.
- Similar symbols: Draw!
We will use our understanding of the game, and our beginner's knowledge of Python, to port the game to a computer system. This will be done with the help of Python conditional statements: 'If', 'Elif', and 'else', and the use of the random module. We will learn to import it and use its functionality to add randomness to our game.
Running a game in Python
Now we will run the game in Python. We will use the concepts of Python arrays and random objects using the Python random module to achieve our goal.
This is how the program will proceed:
The program will ask you to choose a rock, paper or scarf. The computer will periodically choose from 3 options. Depending on the different conditions above, the system will decide who won the game and will give the option to play again.
Defining lists & generating ASCII art
First, we will produce ASCII art with Rock Paper Scissors. We'll keep these internal variables called full, which are stored inside a Python array rps_list.
rock = """
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
"""
paper = """
_______
---' ____)____
______)
_______)
_______)
---.__________)
"""
scissors = """
_______
---' ____)____
______)
__________)
(____)
---.__(___)
"""
rps_list = [rock, paper, scissors ]
Requesting input from the user
The next step is to get input from the user. We will use flexibility user_choice Save what the user chooses to play the game with, and print for the user to see. Note that the dynamic user_choice will store the input as a string. This key point will be useful to remember when we use cases to compare both user decisions and computer decisions in our next article.
user_choice = input("What do you choose? Type 'rock' for Rock, 'scissors' for Scissors and 'paper' for Paper")
print(f"User chooses {user_choice}")
Random computer selection
Once the user has decided their choice, next we will have the computer make a random decision. We will use the random module for this purpose. You can check more about this through the following link:
random – Generate pseudo random numbers
A random module choice() The function allows us to randomly select from a given Python list given as a parameter to it. We will keep this random selection in transition computer_choice and print it.
import random
computer_choice = random.choice(rps_list)
print(f"Computer chooses {computer_choice}")
In addition, you can also check out this article, which tells you how to add randomness to our code using Python's random module. It includes a simple explanation of the different functions with easy-to-understand examples:
How to Use Random with the Python Random Module
Conditions use conditions
Now we will describe all the different situations we described earlier in code form. We will use if, elif, and other, which are detailed Python 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 can be seen in the code above, we used each condition, comparing the computer's choice with the user's choice stored as an input string, and then printed the results, whether the Computer wins, or the Computer wins, or the Computer gets a draw between the two.
Lasting
The program above is a simple python code, easy to understand and provides an introduction to existing Python implementations and the use of the random module, especially its select function.
Although there are many ways in which possible situations are coded, the above was a clear and user-friendly code involving if, elif and the like. Can you think of any other way this game could have been coded?



