Rock Paper Scissors is the perfect first Python game: one random choice from the computer, one choice from you, and a rule that decides who wins. This version is a best-of-five match with a running score, short answers (r, p, s) and a dictionary that holds the rules.
Play a match below, then see how 40 lines of Python run the whole thing.
Play Rock Paper Scissors
Press ▶ Play to start the game. When it asks you something, type your answer and press Enter.
View and edit the code (41 lines)
How to play
- Type r (rock), p (paper) or s (scissors) and press Enter.
- Rock blunts scissors, scissors cut paper, paper wraps rock.
- The first to three round wins takes the match. A tie replays the round.
The complete Rock Paper Scissors code
Here is the whole program (41 lines). It only uses Python's standard library, so it runs anywhere Python 3 is installed.
import random
NAMES = {"r": "rock", "p": "paper", "s": "scissors"}
BEATS = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
def round_result(me, cpu):
if me == cpu:
return "tie"
if BEATS[me] == cpu:
return "win"
return "lose"
print("ROCK PAPER SCISSORS - first to 3 wins")
my_score = 0
cpu_score = 0
while my_score < 3 and cpu_score < 3:
key = input("\nr, p or s? ").strip().lower()
if key not in NAMES:
print("Please type r, p or s.")
continue
me = NAMES[key]
cpu = random.choice(list(BEATS))
print(f"You chose {me}. The computer chose {cpu}.")
result = round_result(me, cpu)
if result == "win":
my_score += 1
print(f"{me.capitalize()} beats {cpu}. You win the round!")
elif result == "lose":
cpu_score += 1
print(f"{cpu.capitalize()} beats {me}. The computer wins the round.")
else:
print("Same choice. It's a tie!")
print(f"Score: you {my_score} - {cpu_score} computer")
if my_score == 3:
print("\nYou win the match!")
else:
print("\nThe computer wins the match. Rematch?")How the Rock Paper Scissors game works, step by step
01Put the rules in a dictionary
Instead of nine if statements, one dictionary says what each choice beats. Looking up BEATS["rock"] gives "scissors". Rules as data are shorter and easier to change.
BEATS = {"rock": "scissors", "scissors": "paper", "paper": "rock"}02Let the computer choose
random.choice() picks one item from a list. list(BEATS) turns the dictionary's keys into ["rock", "scissors", "paper"].
cpu = random.choice(list(BEATS))03Decide one round
Same choice is a tie. If what I picked beats what the computer picked, I win; otherwise I lose. A function that returns "win", "lose" or "tie" keeps the main loop tidy.
def round_result(me, cpu):
if me == cpu:
return "tie"
if BEATS[me] == cpu:
return "win"
return "lose"04Loop until someone reaches 3
A while loop keeps playing rounds while both scores are below three. .strip().lower() forgives extra spaces and capital letters, and continue skips bad input.
while my_score < 3 and cpu_score < 3:
key = input("r, p or s? ").strip().lower()What you'll learn
random.choice()for computer moves- Dictionaries as lookup tables
if/elif/elsechains- A
whileloop with a score condition - Cleaning input with
.strip()and.lower()
Python concepts in this game:
Rock Paper Scissors coding challenges
Write the key parts of the game yourself. Press Run to try your code and Check to test it. Each challenge you pass earns a star (saved in this browser).
Who wins the round?
Write round_result(me, cpu) that returns "win", "lose" or "tie". Use the BEATS dictionary.
Hint
Check for a tie first. Then BEATS[me] == cpu means you win.
Show a solution
BEATS = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
def round_result(me, cpu):
if me == cpu:
return "tie"
if BEATS[me] == cpu:
return "win"
return "lose"
print(round_result("rock", "scissors"))Short answers
Write to_name(key): it turns "r", "p", "s" (in any case, with spaces around) into "rock", "paper", "scissors", and returns None for anything else.
Hint
.strip().lower() cleans the text, and dict.get(key) returns None when the key is missing.
Show a solution
NAMES = {"r": "rock", "p": "paper", "s": "scissors"}
def to_name(key):
return NAMES.get(key.strip().lower())
print(to_name(" R "))Print the score line
The program reads three results with input() (each is win, lose or tie). Count them and print exactly Score: you 2 - 1 computer (with the real numbers).
Hint
Use me += 1 for a win and cpu += 1 for a loss, then an f-string: f"Score: you {me} - {cpu} computer".
Show a solution
me = 0
cpu = 0
for i in range(3):
result = input("Result? ")
if result == "win":
me += 1
elif result == "lose":
cpu += 1
print(f"Score: you {me} - {cpu} computer")Make it your own
Once your challenges pass, try one of these upgrades:
- Add lizard and Spock (each choice then beats two others).
- Let the player pick how many rounds the match lasts.
- Make the computer learn: pick what beats your most common choice.
- Show the result as ASCII-art hands.
Frequently asked questions
How do you code Rock Paper Scissors in Python?
Use random.choice() for the computer, input() for the player, and a dictionary that maps each choice to the one it beats. Compare the two choices to decide the round and keep scores in two variables.
Why use a dictionary instead of if statements?
One dictionary line replaces six comparisons. To add new moves you only add entries, and the round logic does not change.
Is the computer's choice really random?
It uses Python's random module, which is random enough for games. On this page the same random seed is reused while you answer, so the game stays consistent from move to move.