Rock, Paper, Scissors Game with Python

2023

In this project, we will explore how to create a Rock, Paper, Scissors game using Python. The game allows you to play against the computer and test your luck and strategy. We'll explain each step of the code and provide the final working code at the end.


Setting Up the Game

To start, we need to import the random module to generate a random choice for the computer:

import random

This module will enable us to select a random item from a list.

Next, we set up a list of choices for the game:

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

These choices represent the three options available in the game.

Playing the Game

We use a while loop to continue the game until the player decides to stop:

while True:
    computer_choice = random.choice(choices)
    player_choice = input("Enter your choice (rock, paper, scissors): ")

Inside the loop, we select a random choice for the computer using random.choice() and prompt the player to enter their choice using the input() function.

The program then compares the player's choice to the computer's choice and determines the winner using a series of if-elif-else statements:

    if player_choice == computer_choice:
        print("It's a tie!")
    elif player_choice == "rock":
        if computer_choice == "scissors":
            print("You win!")
        else:
            print("You lose!")
    elif player_choice == "paper":
        if computer_choice == "rock":
            print("You win!")
        else:
            print("You lose!")
    elif player_choice == "scissors":
        if computer_choice == "paper":
            print("You win!")
        else:
            print("You lose!")

If the player's choice matches the computer's choice, the game results in a tie. Otherwise, the program checks the different winning combinations to determine the winner.

After each round, the player is asked if they want to play again:

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break

If the player enters "y" or "Y," the game continues with a new round. Otherwise, the break statement exits the loop, ending the game.


Final Code

Here's the complete Python code for the Rock, Paper, Scissors game:

import random

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

while True:
    computer_choice = random.choice(choices)
    player_choice = input("Enter your choice (rock, paper, scissors): ")

    if player_choice == computer_choice:
        print("It's a tie!")
    elif player_choice == "rock":
        if computer_choice == "scissors":
            print("You win!")
        else:
            print("You lose!")
    elif player_choice == "paper":
        if computer_choice == "rock":
            print("You win!")
        else:
            print("You lose!")
    elif player_choice == "scissors":
        if computer_choice == "paper":
            print("You win!")
        else:
            print("You lose!")

    play_again = input("Play again? (y/n): ")
    if play_again.lower() != "y":
        break

That's it! You now have a Python code snippet that allows you to play the Rock, Paper, Scissors game against the computer. Feel free to modify the code or use it as a starting point for your own projects. Have fun playing the game!

Back