Create a text-based rock, paper, scissors game using Python

Creating a rock, paper, scissors game is one of those coding exercises that one would find on almost every other “top ten coding projects for beginners” website. Coding this simple game involves using almost all the basics of programming, such as: conditionals, loops, and I/O operations.

In this tutorial, we’ll develop and code the logic of a text-based rock, paper, scissors game using Python. We’ll create a single-round user vs. computer game, then extend it to support multiple rounds. So, without further ado, let’s start!

User and computer input

The first step is making the user and the computer choose between the three options.

Note: Please enter our choice in the designated area before running the code: 1 for Rock, 2 for Paper, and 3 for Scissors.

import random
options = ['Rock', 'Paper', 'Scissors']
userOption = int(input('Press 1 for Rock, 2 for Paper, 3 for Scissors: \n'))
userChoice = options[userOption-1]
computerChoice = options[random.randint(0,2)]
print(f'You chose: {userChoice}')
print(f'Computer chose: {computerChoice}')

Enter the input below

Code explanation

  • Line 1: We import Python’s random library, which we’ll use to make the computer choose randomly between the three available choices.

  • Line 3: We create an array containing all the possible choices the user can make.

  • Line 5: We prompt the user to choose an option using the input() function. We use the int() function to ensure the conversion of the input string numeral to the type int.

  • Line 4: We map the user’s input to the corresponding option using array indexing for the user’s choice.

  • Line 8: We use the random.randint() function to generate a random number between 0 and 2 (both inclusive). We then map the generated random number to the corresponding option using array indexing for the computer’s choice.

  • Lines 10–11: We display the user’s and computer’s choices.

Game logic

The next step is to create the game logic. This is straightforward; we compare the user and computer choices using nested if statements. Here is the first set of possibilities:

result = "It's a tie!"
if userChoice == "Rock":
if computerChoice == "Paper":
result = "Computer Won!"
elif computerChoice == "Scissors":
result = "You Won!"

Code explanation

  • Line 1: We create a variable to store the result and set the default value to "It's a tie!".

  • Lines 3–7: We consider the case that the user chose Rock, check whether the computer chose Paper or Scissors, and update the result variable accordingly. If the computer also chose Rock, the default value of result will not change. Hence all the cases are catered for.

We’ll now extend this logic to handle the cases where the user chooses Paper or Scissors.

import random
options = ['Rock', 'Paper', 'Scissors']
userOption = int(input('Press 1 for Rock, 2 for Paper, 3 for Scissors: \n'))
userChoice = options[userOption-1]
computerChoice = options[random.randint(0,2)]
print(f'You chose: {userChoice}')
print(f'Computer chose: {computerChoice}')
result = "It's a tie!"
if userChoice == "Rock":
if computerChoice == "Paper":
result = "Computer Won!"
elif computerChoice == "Scissors":
result = "You Won!"
elif userChoice == "Paper":
if computerChoice == "Scissors":
result = "Computer Won!"
elif computerChoice == "Rock":
result = "You Won!"
elif userChoice == "Scissors":
if computerChoice == "Rock":
result = "Computer Won!"
elif computerChoice == "Paper":
result = "You Won!"
print(result)

Enter the input below

  • Lines 15–31: We implement the game logic as discussed above.

  • Line 33: We display the result.

With this, our single-round rock paper scissors game is complete. We can extend this to multiple rounds using a for loop. The player with the most round victories becomes the winner.

Multiple rounds

Let’s modify our game to play multiple rounds before declaring a winner:

import random

options = ['Rock', 'Paper', 'Scissors']

userScore, computerScore = 0, 0
totalRounds = 3

for i in range(totalRounds):
    userOption = int(input('Press 1 for Rock, 2 for Paper, 3 for Scissors: '))
    userChoice = options[userOption-1]

    computerChoice = options[random.randint(0,2)]

    print(f'You chose: {userChoice}')
    print(f'Computer chose: {computerChoice}')

    result = "It's a tie!"

    if userChoice == "Rock":
        if computerChoice == "Paper":
            result = "Computer won this round!"
            computerScore += 1
        elif computerChoice == "Scissors":
            result = "You won this round!"
            userScore += 1

    elif userChoice == "Paper":
        if computerChoice == "Scissors":
            result = "Computer won this round!"
            computerScore += 1
        elif computerChoice == "Rock":
            result = "You won this round!"
            userScore += 1

    elif userChoice == "Scissors":
        if computerChoice == "Rock":
            result = "Computer won this round!"
            computerScore += 1
        elif computerChoice == "Paper":
            result = "You won this round!"
            userScore += 1
    
    print(result + '\n')

print(f'Your score: {userScore}')
print(f"Computer's score: {computerScore}")

if userScore > computerScore:
    print('You won most rounds!')
elif computerScore > userScore:
    print('Computer won most rounds!')
else: 
    print("It's a draw")
Multi-round Rock Paper Scissors game

Code explanation

  • Line 5: We declare two variables to keep track of the user’s and computer’s scores.

  • Line 6: We set the total number of rounds to 3. We can change this to any number we like.

  • Line 8: We start our for loop. The total number of iterations will be the same as the total round, i.e., 3 in this case.

  • Lines 9–43: We move these lines inside the for loop as the choices will now be made and evaluated in every iteration of the for loop.

  • Lines 22, 30, and 38: We increase the computerScore by 1 if the computer wins a round.

  • Lines 25, 33, and 41: We increase the user score by 1 if the user wins a round.

  • Lines 45–46: We display the user and computer scores after all the rounds have been played.

  • Lines 48–53: We compare the two scores and determine the winner accordingly.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved