Build Rock Paper Scissors: Python Project!

Build Rock Paper Scissors: Python Project!

DataScience4

Implementing the Rock Paper Scissors Game in Python

A Beginner's Guide to Your First Python Project

🗂 Category: PROGRAMMING
🕒 Date: 2025-11-27 | ⏱️ Read time: 4 min read

---

Building a classic Rock, Paper, Scissors game is an excellent way for beginners to apply fundamental Python concepts in a fun, interactive project. In this tutorial, we will walk through the creation of this game step-by-step. You will learn how to take user input, generate a random choice for the computer, and use conditional logic to determine the winner.

#### What You'll Learn

• How to use the random module to make a random selection.
• How to get input from a user using the input() function.
• How to use if, elif, and else statements to control the flow of your program.
• Basic string manipulation with the .lower() method.

---

Step 1: Setting Up the Game

First, we need to import the random module, which will allow the computer to pick an option. We also need to define the list of possible choices.

Create a new Python file (e.g., game.py) and add the following lines:

import random

# A list containing the three possible options for the game.
options = ['rock', 'paper', 'scissors']

This sets the foundation for our game. The computer will randomly select one of the items from the options list.

Step 2: Getting the User's Input

Next, we need to prompt the player to make their choice. We can do this using the built-in input() function. It's also good practice to convert the user's input to lowercase to make our comparisons case-insensitive (so "Rock", "ROCK", and "rock" are all treated the same).

# Prompt the user to enter their choice.
user_choice = input("Choose rock, paper, or scissors: ")

# Convert the user's input to lowercase for easier comparison.
user_choice = user_choice.lower()

Step 3: Generating the Computer's Choice

Now it's the computer's turn. We will use the random.choice() function to pick a random element from our options list.

# Select a random choice for the computer from the 'options' list.
computer_choice = random.choice(options)

At this point, both the user and the computer have made their selections. Let's show them to the player.

print(f"\nYou chose: {user_choice}")
print(f"Computer chose: {computer_choice}\n")

Step 4: Determining the Winner with Conditional Logic

This is the core of the game. We need to compare the user_choice with the computer_choice to see who won. We can accomplish this with a series of if, elif, and else statements.

The rules are:
• Rock beats scissors
• Scissors beats paper
• Paper beats rock

Let's implement this logic.

Check for a tie: The simplest case is a tie, where both chose the same option.
Check for winning conditions: We then check all the scenarios where the user wins.
Handle losing: If it's not a tie and the user didn't win, the only remaining possibility is that the computer won.

# Case 1: The game is a tie.
if user_choice == computer_choice:
print("It's a tie!")

# Case 2: The user wins.
# We check all three winning conditions for the user.
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'scissors' and computer_choice == 'paper') or \
(user_choice == 'paper' and computer_choice == 'rock'):
print("You win!")

# Case 3: The user loses.
# If it's not a tie and the user didn't win, the computer must have won.
else:
print("You lose!")

The Complete Code

Here is the full script with all the steps combined.

import random

# Step 1: Define the game options.
options = ['rock', 'paper', 'scissors']

# Step 2: Get the user's choice.
user_choice = input("Choose rock, paper, or scissors: ").lower()

# Add a check for valid input (optional but good practice).
if user_choice not in options:
print("Invalid choice! Please choose rock, paper, or scissors.")
else:
# Step 3: Generate the computer's choice.
computer_choice = random.choice(options)

# Display the choices.
print(f"\nYou chose: {user_choice}")
print(f"Computer chose: {computer_choice}\n")

# Step 4: Determine the winner using conditional logic.
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'scissors' and computer_choice == 'paper') or \
(user_choice == 'paper' and computer_choice == 'rock'):
print("You win!")
else:
print("You lose!")

Next Steps

Congratulations! You have successfully built a Rock, Paper, Scissors game in Python. To practice further, you can try adding more features:

Play again: Put the game logic inside a while loop to allow the user to play multiple rounds.
Keep score: Create variables to track the user's and the computer's scores and display them after each round.
Input validation: Improve the check to ensure the user only enters one of the three valid options.

Report Page