Build Rock Paper Scissors in Python
Code the classic Rock Paper Scissors game in Python against the computer. Learn input, random.choice, comparing strings and keeping score, with complete runnable code, a sample game and a quiz.
Key takeaways
- random.choice(['rock', 'paper', 'scissors']) makes the computer pick a move
- Compare the player's move and the computer's move with if/elif/else to decide the winner
- There are only three ways the player wins — list them and the rest is a loss or tie
- Variables can keep a running score across many rounds
- A while loop lets you play as many rounds as you like
A duel against the computer
Rock Paper Scissors is the perfect second Python project. The rules are tiny — rock beats scissors, scissors beat paper, paper beats rock — but coding them teaches you how to compare values, make decisions, and keep score. By the end you'll have a game you can play against the computer as many times as you like.
If you can already use making decisions with if, you're ready. Let's build it up step by step.
Step 1: let the computer choose
The computer needs to pick rock, paper or scissors at random. random.choice grabs one item from a list:
import random
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
Now computer holds one of the three moves, chosen fairly each time. (More on this in random numbers and games in Python.)
Step 2: get the player's move
We read the player's move with input() and use .lower() so capital letters don't matter:
player = input("rock, paper or scissors? ").lower()
If they type Rock, .lower() turns it into rock, matching our list.
Step 3: decide who wins
This is the heart of the game. First check for a tie. Then list the three ways the player wins. Everything else is a loss:
if player == computer:
print("It's a tie!")
elif (player == "rock" and computer == "scissors") or \
(player == "paper" and computer == "rock") or \
(player == "scissors" and computer == "paper"):
print("You win!")
else:
print("Computer wins!")
There are only three winning combinations, so we can write them all out. The \ at the end of a line just lets one long condition continue onto the next line for readability. (For more on and/or, see python comparison and logic.)
The complete game
Here it is all together, with score keeping and a play-again loop:
import random
choices = ["rock", "paper", "scissors"]
player_score = 0
computer_score = 0
print("=== ROCK PAPER SCISSORS ===")
while True:
player = input("\nrock, paper or scissors? ").lower()
if player not in choices:
print("Please type rock, paper or scissors.")
continue
computer = random.choice(choices)
print(f"Computer chose {computer}.")
if player == computer:
print("It's a tie!")
elif (player == "rock" and computer == "scissors") or \
(player == "paper" and computer == "rock") or \
(player == "scissors" and computer == "paper"):
print("You win this round!")
player_score += 1
else:
print("Computer wins this round!")
computer_score += 1
print(f"Score — You: {player_score} Computer: {computer_score}")
again = input("Play again? (yes/no): ").lower()
if again != "yes":
print("Final score — You:", player_score, "Computer:", computer_score)
print("Thanks for playing!")
break
How the new parts work:
player not in choiceschecks the move is valid. If not,continueskips to the top of the loop and asks again.player_score += 1adds one to the winner's total. The scores survive between rounds because they live outside the loop.- The
\ninside the print adds a blank line so each round is easy to read.
A sample game:
=== ROCK PAPER SCISSORS ===
rock, paper or scissors? rock
Computer chose scissors.
You win this round!
Score — You: 1 Computer: 0
Play again? (yes/no): yes
rock, paper or scissors? paper
Computer chose scissors.
Computer wins this round!
Score — You: 1 Computer: 1
Play again? (yes/no): no
Final score — You: 1 Computer: 1
Thanks for playing!
Why list the wins, not the losses?
Notice we only spelled out the three ways the player wins, then let else cover every loss. That's a neat pattern: when one outcome has fewer cases than the other, check the short list and let else mop up the rest. It keeps your code shorter and easier to read.
Try it yourself
Make the game your own:
- Best of five. Stop the game automatically when someone reaches 3 wins. Use the while loop condition
while player_score < 3 and computer_score < 3:. - Add emojis. Print ✊ for rock, ✋ for paper and ✌️ for scissors using a small dictionary that maps each move to its emoji.
- Lizard and Spock. Add
"lizard"and"spock"to the choices and extend the win rules. Each move now beats two others. - Track ties. Add a
tiescounter and report it at the end alongside the wins.
Quick quiz
Test yourself and earn XP
How does the computer choose its move?
random.choice picks one item at random from the list of three moves.
If the player and computer pick the same move, the result is...
Equal moves mean a tie — we check player == computer first.
Which beats scissors?
Rock crushes scissors. Paper covers rock; scissors cut paper.
Why store wins in a variable like player_score?
A score variable remembers how many rounds each side has won so far.
What lets the game repeat for many rounds?
Wrapping a round in a while loop replays it until the player decides to stop.
FAQ
Call .lower() on their input, like move = input('Your move: ').lower(). That turns ROCK, Rock and rock all into 'rock', so your comparisons always match. You can also check the move is valid and ask again if it isn't.
Yes! Add them to the choices list and extend the rules. Each move now beats two others, so the win conditions get longer. It's a great challenge for practising if/elif logic, and a good reason to organise the rules in a dictionary once you've learned them.
Keep exploring
More in Coding