Rock, Paper, Scissors in Python

"Learn to build a simple Rock, Paper, Scissors game in Python with this beginner-friendly guide."

By Samir Niroula

28 October 2024
Rock, Paper, Scissors in Python

Creating Rock, Paper, Scissors in Python: A Beginner's Guide

Rock, Paper, Scissors is a fun, quick game and a great beginner Python project. This guide will walk you through building a simple text-based version. Let’s jump in!


Step 1: Import Libraries

First, we’ll use Python’s built-in random module to let the computer make random choices.

Code:

import random

Explanation: The random module will allow us to randomly pick Rock, Paper, or Scissors for the computer’s move.


Step 2: Define the Options

We’ll define the available choices (Rock, Paper, Scissors) in a list so we can access them easily.

Code:

choices = ["rock", "paper", "scissors"]

Explanation: Each item in the list represents a possible choice for both the player and the computer.


Step 3: Get Player’s Choice

Let’s write a function to get the player’s choice and ensure they enter a valid option.

Code:

def get_player_choice():
    player_choice = input("Enter rock, paper, or scissors: ").lower()
    while player_choice not in choices:
        print("Invalid choice. Please try again.")
        player_choice = input("Enter rock, paper, or scissors: ").lower()
    return player_choice

Explanation: This function asks the player for their choice and checks if it’s valid. If it’s not, it asks again until a valid input is provided.


Step 4: Generate Computer’s Choice

Now, let’s get a random choice for the computer using random.choice().

Code:

def get_computer_choice():
    return random.choice(choices)
 

Explanation: This function randomly selects Rock, Paper, or Scissors from our choices list for the computer.


Step 5: Determine the Winner

We’ll create a function to compare the player’s choice with the computer’s choice and decide the winner.

Code:

def determine_winner(player, computer):
    if player == computer:
        return "It's a tie!"
    elif (player == "rock" and computer == "scissors") or \
         (player == "scissors" and computer == "paper") or \
         (player == "paper" and computer == "rock"):
        return "You win!"
    else:
        return "Computer wins!"
 
    **Explanation:** This function checks all possible winning conditions for the player. If none match, the computer wins.

Step 6: Play the Game

Finally, we’ll put everything together in a main function that runs the game.

Code:

def play_game():
    print("Welcome to Rock, Paper, Scissors!")
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()
 
    print(f"You chose: {player_choice}")
    print(f"Computer chose: {computer_choice}")
 
    result = determine_winner(player_choice, computer_choice)
    print(result)
 

Explanation: This function runs the entire game: it gets the player’s and computer’s choices, shows both choices, and displays the result.


Step 7: Play Again Option (Optional)

If you want, you can add a loop to allow the player to play multiple rounds.

Code:

while True:
    play_game()
    play_again = input("Do you want to play again? (yes/no): ").lower()
    if play_again != "yes":
        print("Thanks for playing!")
        break
 

Explanation: This loop runs play_game() repeatedly until the player chooses not to play again.


Full Code

Here’s the complete code for the game:

import random
 
choices = ["rock", "paper", "scissors"]
 
def get_player_choice():
    player_choice = input("Enter rock, paper, or scissors: ").lower()
    while player_choice not in choices:
        print("Invalid choice. Please try again.")
        player_choice = input("Enter rock, paper, or scissors: ").lower()
    return player_choice
 
def get_computer_choice():
    return random.choice(choices)
 
def determine_winner(player, computer):
    if player == computer:
        return "It's a tie!"
    elif (player == "rock" and computer == "scissors") or \
         (player == "scissors" and computer == "paper") or \
         (player == "paper" and computer == "rock"):
        return "You win!"
    else:
        return "Computer wins!"
 
def play_game():
    print("Welcome to Rock, Paper, Scissors!")
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()
 
    print(f"You chose: {player_choice}")
    print(f"Computer chose: {computer_choice}")
 
    result = determine_winner(player_choice, computer_choice)
    print(result)
 
while True:
    play_game()
    play_again = input("Do you want to play again? (yes/no): ").lower()
    if play_again != "yes":
        print("Thanks for playing!")
        break

Output

Welcome to Rock, Paper, Scissors!
Enter rock, paper, or scissors: rock
You chose: rock
Computer chose: scissors
You win!
 
Do you want to play again? (yes/no): yes
 
Welcome to Rock, Paper, Scissors!
Enter rock, paper, or scissors: paper
You chose: paper
Computer chose: rock
You win!
 
Do you want to play again? (yes/no): yes
 
Welcome to Rock, Paper, Scissors!
Enter rock, paper, or scissors: scissors
You chose: scissors
Computer chose: scissors
It's a tie!
 
Do you want to play again? (yes/no): no
Thanks for playing!

Conclusion

Congratulations! You’ve created a Rock, Paper, Scissors game in Python. This project helps beginners learn user input, conditional statements, and basic functions. Keep experimenting with the game to add more features and improve your skills!