GITHUB REPO | FOLLOW ME ON -> TWITTER | LINKEDIN

Python Programming: Build Your Own Rock, Paper, Scissors Game

In this project, we will use Python to develop a simple game of Rock, Paper, Scissors. The user can choose an option, and the computer will choose one at random. The winner will then be selected per the game’s regulations.

Al-Junaed Islam

--

Photo by Marcus Wallis on Unsplash

Step-by-step Code Explanation:

The first thing we need to do is set up our Python Rock-Paper-Scissors. Open up your preferred code editor, create a new file, and save it with a descriptive name such as “rock_paper_scissors.py.

First, we import the random module, which will be used to generate random numbers. After that, we will copy the ASCII art of rock, paper, and scissors from this gist. You can copy from below code too.

import random

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

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

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

Using an input statement that accepts an integer value, we ask the user to choose an option. Then, to replicate the computer’s option, we use the randint() function from the random module to generate a random number between 0 and 2.

user_played = int(input('What do you want to play? Type 0 for "Rock", 1 for "Paper" or 2 for "Scissiors"\n'))

computer_played = random.randint(0,2)

We apply an if-elif-else statement set to identify the winner based on the user’s input and the computer’s random pick. We compare the user’s input to the computer’s option, then display the outcome together with a relevant message and a display of both the user’s and the computer’s choices.

print("You played:")
if user_played == 0:
print("Rock\n"+rock)

print("Computer played:")
if computer_played == 0:
print("Rock\n"+rock)
print("DRAW!")
elif computer_played == 1:
print("Paper\n"+paper)
print("YOU LOSE!")
else:
print("Scissors\n"+scissors)
print("YOU WIN!")

elif user_played == 1:
print("Paper\n"+paper)

print("Computer played:")
if computer_played == 0:
print("Rock\n"+rock)
print("YOU WIN!")
elif computer_played == 1:
print("Paper\n"+paper)
print("DRAW!")
else:
print("Scissors\n"+scissors)
print("YOU LOSE!")

elif user_played == 2:
print("Scissors\n"+scissors)

print("Computer played:")
if computer_played == 0:
print("Rock\n"+rock)
print("YOU LOSE!")
elif computer_played == 1:
print("Paper\n"+paper)
print("YOU WIN!")
else:
print("Scissors\n"+scissors)
print("DRAW!")

else:
print("Wrong Selection. Try Again.")

Here is the full code.

rock_paper_scissors.py

Test Output

Test Output

Features

Program’s Features

  • Takes user input to select rock, paper, or scissors
  • Generates a random selection for the computer
  • Determines the winner based on the game’s rules

Python Concepts and Techniques Used

  • Importing modules
  • Multi-line strings as ASCII arts
  • User input using the input() function
  • Conditional statements using if-elif-else
  • Generating random numbers using the randint() function

We learnt how to make a small text-based game using Python in this project. To create random numbers, we applied the random module, multi-line strings to ASCII arts, and if-elif-else statements to select the winner. This project is a great opportunity to begin learning about user input, conditional statements, and the random module.

This article is a part of my Python Project Journal. To learn more about my journey in learning Python and to see my other projects, check out this project journal. Thank you!

--

--