Editor

Python Program for Rock, Paper, Scissors

Beginner · Games and random numbers

Rock, paper, scissors is a game for two players: rock beats scissors, scissors beats paper, paper beats rock, and the same choice is a draw. In this program the computer picks its move at random and plays five rounds against a list of player moves, keeping score.

Program

rock_paper_scissors.py
import random
def winner(player, computer):
if player == computer:
return "Draw"
if player == "rock" and computer == "scissors":
return "You win"
if player == "scissors" and computer == "paper":
return "You win"
if player == "paper" and computer == "rock":
return "You win"
return "Computer wins"
options = ["rock", "paper", "scissors"]
# Sample player moves. With input() you would ask in each round:
# player = input("rock, paper or scissors? ").lower()
player_moves = ["rock", "paper", "scissors", "rock", "paper"]
your_score = 0
computer_score = 0
for player in player_moves:
computer = random.choice(options)
result = winner(player, computer)
print("You:", player, "| Computer:", computer, "->", result)
if result == "You win":
your_score += 1
elif result == "Computer wins":
computer_score += 1
print("Final score: you", your_score, "- computer", computer_score)
Output
You: rock | Computer: paper -> Computer wins
You: paper | Computer: paper -> Draw
You: scissors | Computer: scissors -> Draw
You: rock | Computer: scissors -> You win
You: paper | Computer: scissors -> Computer wins
Final score: you 1 - computer 2

How it works

  1. random.choice(options) picks the computer move from the list, so the results change on every run.
  2. winner() checks for a draw first, then the three ways the player can win; any other pairing means the computer won.
  3. The loop plays one round for each move in player_moves and adds a point to whoever won that round.
  4. Keeping the rules in one function keeps the scoring loop short and easy to read.

Another way to write it

Using a dictionary of what beats what

Using a dictionary of what beats what
import random
beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
for player in ["rock", "paper", "scissors"]:
computer = random.choice(list(beats))
if player == computer:
result = "Draw"
elif beats[player] == computer:
result = "You win"
else:
result = "Computer wins"
print(player, "vs", computer, "->", result)
Output
rock vs rock -> Draw
paper vs rock -> You win
scissors vs rock -> Computer wins

beats[player] is the one move that player defeats, so a single comparison replaces the three if checks in winner().

Try it yourself

Make the game best of three: stop as soon as either side reaches 2 points.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises