DEV Community

Codes With Pankaj
Codes With Pankaj

Posted on

Python game: Rock, Paper, Scissors

We're going to dive headfirst into creating your very first Python game: Rock, Paper, Scissors.

Let's Get Started:

Open your Python IDLE and create a new file named rps.py. Now, let's start coding:

from random import randint

# Create a list of play options
t = ["Rock", "Paper", "Scissors"]

# Assign a random play to the computer
computer = t[randint(0,2)]

# Set player to False
player = False

# Game Loop
while player == False:
    # Set player to True
    player = input("Rock, Paper, Scissors?")

    # Check for a tie
    if player == computer:
        print("It's a tie!")

    # Check for player winning scenarios
    elif player == "Rock":
        if computer == "Paper":
            print("You lose!", computer, "covers", player)
        else:
            print("You win!", player, "smashes", computer)

    elif player == "Paper":
        if computer == "Scissors":
            print("You lose!", computer, "cut", player)
        else:
            print("You win!", player, "covers", computer)

    elif player == "Scissors":
        if computer == "Rock":
            print("You lose...", computer, "smashes", player)
        else:
            print("You win!", player, "cut", computer)

    # Handle invalid inputs
    else:
        print("That's not a valid play. Check your spelling!")

    # Reset player status to continue the loop
    player = False
    computer = t[randint(0,2)]
Enter fullscreen mode Exit fullscreen mode

Understanding the Code:

Let's dissect what's happening in our code:

  1. We import randint from the random module to generate random integers, which we'll use to determine the computer's play.

  2. We create a list, t, containing the play options: "Rock", "Paper", and "Scissors".

  3. The computer's play is randomly selected from the list.

  4. We set the initial state of the player to False to indicate that they haven't made a play yet.

  5. The game loop (while loop) runs until the player makes a valid play.

  6. Inside the loop, we prompt the player for their choice using the input() function.

  7. Based on the player's input, we compare it with the computer's play to determine the outcome of the game.

  8. We handle different scenarios such as a tie, player wins, or invalid inputs.

  9. Finally, we reset the player status to False to continue the loop and generate a new play for the computer.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay