Introduction to the Game
has been an interesting and on-the-go game since our childhood, one we could play anywhere when bored. The game is simple. It involves two players, and each player has to choose one of the 3 options: Rock, Paper, or Scissors. Rock is expressed using a fist, Scissors with the two fingers spread out, and Paper with the hand opened flat.
The following are the different scenarios that can happen and what they would 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 will use our understanding of the game, as well as our beginner’s knowledge of Python, to code this game into a computer program. This will be done with the help of Python conditional statements: ‘if’, ‘elif’, and ‘else’, as well as the use of the random module, which is an in-built Python module. We will learn to import it and use its function to include the element of randomness in our game.
Implementing the Game in Python
Now we will implement the game in Python. We will use the concepts of Python Lists and Randomisation using the Python random module to achieve our goal.
This is how the program will proceed:
The program will ask you to choose Rock, Paper or Scissors. The computer will randomly choose one of the 3 choices. Based on the different scenarios above, the program will decide who won the game and will give an option to play again.
Defining the List & Generating the ASCII Art
First, we will generate the ASCII Art for Rock Paper Scissors. We will store these inside variables named correspondingly, which are further stored inside a Python List rps_list.
rock = """
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
"""
paper = """
_______
---' ____)____
______)
_______)
_______)
---.__________)
"""
scissors = """
_______
---' ____)____
______)
__________)
(____)
---.__(___)
"""
rps_list = [rock, paper, scissors ]
Asking for Input from the User
The next step is to get the input from the user. We will use the variable user_choice to store what the user chooses to play the game with, as well as print it out for the user to see. Notice that the variable user_choice will store the input as a string. This key point will be useful to remember when we use conditionals to compare both the user’s and the computer’s choices in our article ahead.
user_choice = input("What do you choose? Type 'rock' for Rock, 'scissors' for Scissors and 'paper' for Paper")
print(f"User chooses {user_choice}")
Computer’s Random Choice
Once the user has decided their choice, next we will make the computer make a random choice. We will use the random module for this purpose. You can check more about this through the following link:
random — Generate pseudo-random numbers
The random module’s choice() function allows us to randomly make a choice from a given Python list that has been given as a parameter to it. We will store this random choice in the variable computer_choice and print it out.
import random
computer_choice = random.choice(rps_list)
print(f"Computer chooses {computer_choice}")
Moreover, you can also check this article, that tells you how to include randomisation in our code using Python’s random module. It includes an easy explanation of the different functions with easy-to-understand examples:
How to Implement Randomization with the Python Random Module
Scenarios using Conditionals
Now we will define all the different scenarios that we mentioned in the beginning in code form. We will use if, elif, and else, which 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 can be seen from the code above, we have utilized each and every scenario, comparing the computer’s choice with the user’s choice that had been stored as a string as can be understood from the inverted comma, and then printed out the results, whether the user wins, or the computer wins or it has resulted in a draw between them both.
Conclusion
The above program is a simple Python code, which is easy to understand and gives an introduction to Python’s conditionals and the use of the random module, specifically its choice function.
Although there are a number of ways in which the scenarios could have been coded, the above was an explicit and beginner-friendly code involving the if, elif and else conditionals. Can you think about any other way this game could have been coded?